2 回答
TA贡献1780条经验 获得超5个赞
通过试验核心位置框架经过数月的试验和错误后,即使应用程序被杀死/暂停,我也找到了获取位置更新的解决方案。它适用于iOS 7和8。
这是解决方案: -
如果您的应用是基于位置的移动应用,需要在设备发生重大变化时监控设备的位置,当设备距离上一个已知位置移动超过500米时,iOS会返回一些位置坐标。是的,即使应用程序被用户或iOS本身杀死/暂停,您仍然可以获得位置更新。
因此,locationManager
即使应用程序被杀死/暂停,为了获得位置更新,您必须使用该方法startMonitoringSignificantLocationChanges
,您不能使用startUpdatingLocation
。
当iOS想要将位置更新返回给应用程序时,它将帮助您重新启动应用程序并将密钥返回UIApplicationLaunchOptionsLocationKey
到应用程序委托方法didFinishLaunchingWithOptions
。
关键UIApplicationLaunchOptionsLocationKey
是非常重要的,你必须知道如何处理它。您必须在收到密钥时创建新的locationManager实例,并且您将获得locationManager委托方法的位置更新didUpdateLocations
。
以下是示例代码: -
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ self.shareModel = [LocationShareModel sharedModel]; if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]) { self.shareModel.anotherLocationManager = [[CLLocationManager alloc]init]; self.shareModel.anotherLocationManager.delegate = self; self.shareModel.anotherLocationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; self.shareModel.anotherLocationManager.activityType = CLActivityTypeOtherNavigation; if(IS_OS_8_OR_LATER) { [self.shareModel.anotherLocationManager requestAlwaysAuthorization]; } [self.shareModel.anotherLocationManager startMonitoringSignificantLocationChanges]; } return YES; }
除了didFinishLaunchingWithOptions
方法之外,我还在locationManager
应用程序处于活动状态时创建了实例。以下是一些代码示例:
- (void)applicationDidEnterBackground:(UIApplication *)application{ [self.shareModel.anotherLocationManager stopMonitoringSignificantLocationChanges]; if(IS_OS_8_OR_LATER) { [self.shareModel.anotherLocationManager requestAlwaysAuthorization]; } [self.shareModel.anotherLocationManager startMonitoringSignificantLocationChanges];}- (void)applicationDidBecomeActive:(UIApplication *)application{ if(self.shareModel.anotherLocationManager) [self.shareModel.anotherLocationManager stopMonitoringSignificantLocationChanges]; self.shareModel.anotherLocationManager = [[CLLocationManager alloc]init]; self.shareModel.anotherLocationManager.delegate = self; self.shareModel.anotherLocationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; self.shareModel.anotherLocationManager.activityType = CLActivityTypeOtherNavigation; if(IS_OS_8_OR_LATER) { [self.shareModel.anotherLocationManager requestAlwaysAuthorization]; } [self.shareModel.anotherLocationManager startMonitoringSignificantLocationChanges];}
我写了一篇文章解释有关如何获取iOS 7和8的位置更新的详细信息,即使应用程序被杀死/暂停也是如此。我还在GitHub上传了完整的源代码,其中包含如何测试此解决方案的步骤。
TA贡献1886条经验 获得超2个赞
locationManager = [[CLLocationManager alloc] init];#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)if(IS_OS_8_OR_LATER){ [locationManager requestWhenInUseAuthorization];}locationManager.delegate = self;locationManager.distanceFilter = kCLDistanceFilterNone; //whenever we movelocationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;[locationManager startUpdatingLocation];
该代码用户位置仅更新forground app运行但不运行后台运行
[locationManager requestWhenInUseAuthorization];
- 2 回答
- 0 关注
- 763 浏览
添加回答
举报