定期后台位置更新iOS

时间:2015-11-26 12:33:15

标签: ios objective-c background cllocation

我觉得我已经在这个问题上阅读了很多帖子,许多答案各不相同,这使得事情变得非常困难。

简而言之,我想在后台更新用户位置,并定期将结果上传到API(理想情况下每90秒到5分钟)。经过一些挖掘后,我定期更新位置但是当我来调用NSOject类时会出现问题,而NSOject类又在 didUpdateToLocation 中运行 NSURLSessionDataTask (仅在NSDate已过期,因此不是每次调用该方法时都会将整个过程计时。

从测试中我发现通常设备运行此方法两次然后失败。我猜是因为操作系统正在告诉应用程序,因为呼叫太重了。

以下是我的Location方法和AppDelegate方法的样子......

AppDelege

-(void)applicationWillEnterForeground:(UIApplication *)application {
    [self applicationUpdateLocationStatus:true];

}

-

-(void)applicationDidEnterBackground:(UIApplication *)application {
    backgroundTask = [[UIApplication  sharedApplication] beginBackgroundTaskWithExpirationHandler:^{        
        [[UIApplication sharedApplication] endBackgroundTask:backgroundTask];
        backgroundTask = UIBackgroundTaskInvalid;

    }];

    [self applicationUpdateLocationStatus:false];

}

-

-(void)applicationUpdateLocationStatus:(BOOL)foreground {
    backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        [[UIApplication sharedApplication] endBackgroundTask:backgroundTask];
        backgroundTask = UIBackgroundTaskInvalid;

    }];

    if ([self.location locationAllowed] && [self.user settingsVisible] && [[self.user userAccess] length] > 0) {
          [self.location locationApplicationStatus:foreground];

    }

}

位置对象

注意:从其他帖子开始和停止locationManager显然是关键

-(void)locationApplicationStatus:(BOOL)forground {
    [self.locationManager stopUpdatingLocation];
    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyThreeKilometers];
    [self.locationManager setDistanceFilter:5];
    [self.locationManager setPausesLocationUpdatesAutomatically:false];
    [self.locationManager setActivityType:CLActivityTypeOther];
    [self.locationManager startUpdatingLocation];

}

在这里,我尝试使用startMonitoringSignificantLocationChanges,但它没有经常更新,同样的问题仍然存在。

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    [self.locationManager stopUpdatingLocation];

}

-

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    self.nearby = [[NearbyObject alloc] init];

    self.currentLatitude = newLocation.coordinate.latitude;
    self.currentLongitude = newLocation.coordinate.longitude;
    self.currentAccuracy = newLocation.horizontalAccuracy;

    [self.nearby queryNearby:[[CLLocation alloc] initWithLatitude:self.currentLatitude longitude:self.currentLongitude] completion:^(NSError *error, NSArray *content) {
        //SAVE RETURN DATA HERE & EXPIRY
    }];


}

附近对象

-(void)queryNearby:(CLLocation *)location completion:(void (^)(NSError *error, NSArray *content))completion {

    [[NSOperationQueue mainQueue] addOperationWithBlock:^{                       
            sessionConfigure = [NSURLSessionConfiguration defaultSessionConfiguration];
            session = [NSURLSession sessionWithConfiguration:sessionConfigure delegate:nil delegateQueue:[NSOperationQueue mainQueue]];

            sessionURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@user/usernearby.php" ,APP_BASE_URL]];
            sessionTask = [session dataTaskWithURL:sessionURL completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
            //COMPLETION HANDLER OUTPUT 

            }];

            [sessionTask resume];

     }];

}

注意:我已在应用启动时调用[self.locationManager requestAlwaysAuthorization];,并且用户已授予权限。

0 个答案:

没有答案