iOS更新位置,即使应用程序终止
我试图更新用户的位置,即使应用程序被终止。 我添加了地图和背景模式 – >位置更新到我的.plist,我设置了一个本地通知,当位置更新时将触发。 但它从来没有被解雇。 我在AppDelegat.h中有这个:
@interface AppDelegate : UIResponder <CLLocationManagerDelegate>{ CLLocationManager *locationManager; }
和AppDelegate.m中
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions { locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self; locationManager.distanceFilter = kCLDistanceFilterNone; locationManager.desiredAccuracy = kCLLocationAccuracyBest; [locationManager requestAlwaysAuthorization]; [locationManager startMonitoringSignificantLocationChanges]; }
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { UILocalNotification *notification = [[UILocalNotification alloc]init]; notification.repeatInterval = NSDayCalendarUnit; [notification setAlertBody:@"Location update!"]; [notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]]; [notification setTimeZone:[NSTimeZone defaultTimeZone]]; [[UIApplication sharedApplication] scheduleLocalNotification:notification]; }
我在模拟器上用Freeway驱动器testing了这个代码作为位置,它不起作用。
编辑
如果我把代码放在applicationDidEnterBackground
授权请求将closures第二个打开,因为我使用didFinishLaunchingWithOptions
。 我知道这是跟踪位置24小时,因为即使应用程序终止,也有GPS符号。
编辑2
在我的手机上testing之后,代码就可以运行。 它唤醒终止的应用程序并发送本地通知。 它不能在模拟器上工作,但在现实生活中(设备):)
看起来你正在使用CLLocationManagerDelegate的弃用函数:
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { }
相反,你应该实施
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { }