Ios 定期更新后台位置
我觉得我已经读了很多关于这个问题的帖子,很多答案都不一样,这让事情变得非常困难 简而言之,我希望在后台更新用户位置,并定期(理想情况下每90秒到5分钟)将结果上传到API。在一些挖掘之后,我会定期更新位置,但当我调用一个NSOject类时问题就会出现,该类反过来在didUpdateToLocation中运行一个NSURLSessionDataTask(只有在NSDate过期时才会执行,因此不是每次调用该方法时都会执行),它会对整个过程进行计时 通过测试,我发现设备通常会运行此方法两次,然后失败。我猜是因为操作系统让应用程序安静下来,因为电话太重了 下面是我的位置方法和AppDelegate方法的外观 AppDelegeIos 定期更新后台位置,ios,objective-c,background,cllocation,Ios,Objective C,Background,Cllocation,我觉得我已经读了很多关于这个问题的帖子,很多答案都不一样,这让事情变得非常困难 简而言之,我希望在后台更新用户位置,并定期(理想情况下每90秒到5分钟)将结果上传到API。在一些挖掘之后,我会定期更新位置,但当我调用一个NSOject类时问题就会出现,该类反过来在didUpdateToLocation中运行一个NSURLSessionDataTask(只有在NSDate过期时才会执行,因此不是每次调用该方法时都会执行),它会对整个过程进行计时 通过测试,我发现设备通常会运行此方法两次,然后失败。
-(void)applicationWillEnterForeground:(UIApplication *)application {
[self applicationUpdateLocationStatus:true];
}
-
-
定位对象
-(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];
}];
}
注意:从其他帖子中,启动和停止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)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]代码>当应用程序启动且用户已授予权限时 您正在applicationIdentinterbackground
和applicationUpdateLocationStatus
中启动长时间运行的后台任务,但在这两种情况下都不会结束后台任务,因此这将导致您耗尽后台执行时间。@Paulw11我应该在哪里调用它?在完成处理程序之后?不,只要您完成了后台任务,那么在数据会话完成之后
-(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];
}];
}