ios - 如果位置更改或不使用 AFN 3,如何定期向服务器发送后台位置?

标签 ios objective-c afnetworking-3

我是 iOS 应用程序开发的新手。我将使用 AFNetworking 3.0。我想在后台更新位置并将其发送到服务器。在将其发送到服务器时,我想要条件: 1)如果位置没有改变我想调用服务sendlocation1 2)如果位置改变我想调用服务sendlocation2。 位置改变(接近约50米)。 请帮我.... 我尝试了以下代码---- 在ViewDidLoad中:

(void)viewDidLoad {
[super viewDidLoad];

locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];

[locationManager requestAlwaysAuthorization]; 



float Lat = locationManager.location.coordinate.latitude;
float Long = locationManager.location.coordinate.longitude;
NSLog(@"Lat : %f  Long : %f",Lat,Long);}

我要检查,如果我从服务器收到的响应是登录成功,那么只将位置发送到服务器,这就是我使用 if-else 语句的原因。如下所示:

if ([_str isEqualToString:@"Login Success"]) {

        UIAlertController *Loginalert=   [UIAlertController

                                          alertControllerWithTitle:@"Login Success"

                                          message:@"This app is going to close now"

                                          preferredStyle:UIAlertControllerStyleAlert];


        UIAlertAction* yesButton = [UIAlertAction

                                    actionWithTitle:@"Ok"

                                    style:UIAlertActionStyleDefault

                                    handler:^(UIAlertAction * action)

                                    {
                                        if () {
                                            [self sendlocation1];
                                        } else {
                                            [self sendlocation2];
                                        }

                                        [Loginalert dismissViewControllerAnimated:YES completion:nil];



                                    }];

        [Loginalert addAction: yesButton];




    } else {

        NSLog(@"Something Wrong");

最后

(void)sendlocation1{
}

(void)sendlocation2{
}

请帮帮我...如何检查位置是否改变? ...我应该在 if 条件中写什么并将其发送到服务器。

最佳答案

第一步

 (void)viewDidLoad {
[super viewDidLoad];

locationManager = [[CLLocationManager alloc] init];
//set the amount of metres travelled before location update is made
[locationManager setDistanceFilter:50];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];

[locationManager requestAlwaysAuthorization]; 

// call the timer with 5 minutes cap using 5 * 60 = 300
[NSTimer scheduledTimerWithTimeInterval:300.0f target:self selector:@selector(sendlocation1) userInfo:nil repeats:YES];

第二步

每 50 米更换设备此方法称为:

 -(void)locationManager:(CLLocationManager *)manager
   didUpdateToLocation:(CLLocation *)newLocation
          fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"%f",newLocation.coordinate.latitude);

    NSLog(@"%f",newLocation.coordinate.longitude);
    CLLocationDistance meters = [newLocation distanceFromLocation:oldLocation];
         if(meters >=50)
      {
         // call webservice for location is updated 
         [self sendlocation1];
      }else
      {
       // call normal method
       [self webservice_UpdateLocation];
       }
    }
}

-(void)webservice_UpdateLocation
{
    if ([_str isEqualToString:@"Login Success"]) {
        
        UIAlertController *Loginalert=   [UIAlertController
                                          
                                          alertControllerWithTitle:@"Login Success"
                                          
                                          message:@"This app is going to close now"
                                          
                                          preferredStyle:UIAlertControllerStyleAlert];

        
        UIAlertAction* yesButton = [UIAlertAction
                                    
                                    actionWithTitle:@"Ok"
                                    
                                    style:UIAlertActionStyleDefault
                                    
                                    handler:^(UIAlertAction * action)
                                    
                                    {
                                        
                                          
                                        
                                            [self sendlocation2];
                                       
                                        
                                        [Loginalert dismissViewControllerAnimated:YES completion:nil];
                                        
                                        
                                        
                                    }];
        
        [Loginalert addAction: yesButton];
        

        
        
    } else {
        
        NSLog(@"Something Wrong");
 }

关于ios - 如果位置更改或不使用 AFN 3,如何定期向服务器发送后台位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37315226/

相关文章:

ios - 为参数 "invalid value"提供了一个 'appIdName'?

ios - 通过试用购买自动续订购买后应用程序崩溃

ios - 通过 AFNetworking 中的参数的对象数组

ios - 使用 AFNetworking 3.0 从 URL 设置 TableView Cell 图像(异步)

ios - 如何在 Firebase iOS 中过滤搜索

ios - 隐藏导航 Controller 搜索栏并以编程方式打开大标题

ios - Objective-C 仅在触摸时填充特定区域的颜色

iphone - 使用非零原点初始化 UIImageView 的更好方法

iphone - 了解 UIViewController 层次结构

ios - AFNetworking 3.0 setImageWithURLRequest 下载进度