我正在尝试在后台发布数据,我希望在常规时间间隔内收到响应,以便我尝试使用CLLocation Manager。但didUpdateToLocation方法只调用一次。 请建议我! 在这里输入代码
- (void)applicationDidEnterBackgroundUIApplication *)application
{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyBest; // 100 m
[locationManager startUpdatingLocation];
UIApplication* app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_ QUEUE_PRIORITY_DEFAULT, 0), ^{
//Do the work associated with the task
// code for Posting Data Here
NSLog(@"backgroundTimeRemaining: %f", [[UIApplication sharedApplication] backgroundTimeRemaining]);
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
}
- (void)locationManagerCLLocationManager *)manager
didUpdateToLocationCLLocation *)newLocation
fromLocationCLLocation *)oldLocation
{
NSLog(@"Location Change");
}
This Method Call Only Once.
答案 0 :(得分:0)
确保您没有在模拟器中测试它。 您无法在iphone模拟器上获得位置更新。
答案 1 :(得分:0)
要在后台运行位置服务,您需要修改plist,使UIBackgroundModes键包含位置。
答案 2 :(得分:0)
电话应该是
[locationManager startMonitoringSignificantLocationChanges];