我的应用在使用时和在后台运行时需要经常更新位置。
我已经设置了背景模式 - >我的应用程序的位置更新,但我的应用程序在进入后台模式后大约10秒钟停止接收位置更新。
我的代码:
func initializeLocationManager()
{
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.distanceFilter = 1
}
func startLocationManager()
{
locationManager.startUpdatingLocation()
}
我还在info.plist中添加了以下内容:
NSLocationAlwaysUsageDescription
任何帮助将不胜感激。
感谢。
答案 0 :(得分:1)
这解决了我的问题:
if #available(iOS 9.0, *) {
locationManager.allowsBackgroundLocationUpdates = true
} else {
// Fallback on earlier versions
}
答案 1 :(得分:0)
要获得一致的更新,您可能必须每5分钟执行一次操作。如何操作描述为here和here。
以下是Ricky here的解决方案: -
如果您的应用是基于位置的移动应用,需要在设备发生重大变化时监控设备的位置,当设备距离上一个已知位置移动超过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上传了完整的源代码,其中包含如何测试此解决方案的步骤。
请访问以下网址以获取更多信息: -
1. Getting Location Updates for iOS 7 and 8 when the App is Killed/Terminated/Suspended