ios iOS9 中的 CLLocationManager 中的 allowedBackgroundLocationUpdates

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30808192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 06:24:02  来源:igfitidea点击:

allowsBackgroundLocationUpdates in CLLocationManager in iOS9

iosios9core-locationcllocationmanager

提问by tounaobun

I'm using CoreLocationframework in my app in Xcode7(pre-released),and I noticed that there is a newly added property called allowsBackgroundLocationUpdatesin CLLocationManagerclass.

CoreLocation在 Xcode7(预发布)的应用程序中使用框架,我注意到有一个新添加的属性allowsBackgroundLocationUpdatesCLLocationManager类中调用。

What's the relationship between this property and the location updates in the Background Modes of the Capabilities tab. What's the default value of it and does it affect apps running in iOS9?

此属性与“功能”选项卡的“背景模式”中的位置更新之间有什么关系。它的默认值是多少,它是否会影响在 iOS9 中运行的应用程序?

回答by Felix

This new property is explained in the WWDC session "What's New in Core Location".

这个新属性在 WWDC 会议“核心位置的新特性”中进行了解释。

The default value is NOif you link against iOS 9.

默认值是NO如果您针对 iOS 9 进行链接。

If your app uses location in the background (without showing the blue status bar) you have to set allowsBackgroundLocationUpdatesto YESin addition to setting the background mode capability in Info.plist. Otherwise location updates are only delivered in foreground. The advantage is that you can now have location managers with background location updates and otherlocation managers with only foreground location updates in the same app. You can also reset the value to NOto change the behavior.

如果您的应用程序在后台使用位置(不显示蓝色状态栏),除了在 Info.plist 中设置后台模式功能外,您还必须设置allowsBackgroundLocationUpdatesYES。否则位置更新仅在前台提供。优点是您现在可以在同一应用程序中拥有具有后台位置更新的位置管理器和仅具有前台位置更新的其他位置管理器。您还可以将值重置为NO以更改行为。

The documentation is pretty clear about it:

文档对此非常清楚:

By default, this is NO for applications linked against iOS 9.0 or later, regardless of minimum deployment target.

With UIBackgroundModes set to include "location" in Info.plist, you must also set this property to YES at runtime whenever calling -startUpdatingLocation with the intent to continue in the background.

Setting this property to YES when UIBackgroundModes does not include "location" is a fatal error.

Resetting this property to NO is equivalent to omitting "location" from the UIBackgroundModes value. Access to location is still permitted whenever the application is running (ie not suspended), and has sufficient authorization (ie it has WhenInUse authorization and is in use, or it has Always authorization). However, the app will still be subject to the usual task suspension rules.

See -requestWhenInUseAuthorization and -requestAlwaysAuthorization for more details on possible authorization values.

默认情况下,对于与 iOS 9.0 或更高版本链接的应用程序,无论最低部署目标如何,这都是 NO。

将 UIBackgroundModes 设置为在 Info.plist 中包含“location”,您还必须在运行时将此属性设置为 YES,无论何时调用 -startUpdatingLocation 并意图在后台继续。

当 UIBackgroundModes 不包含“位置”时,将此属性设置为 YES 是一个致命错误。

将此属性重置为 NO 等效于从 UIBackgroundModes 值中省略“位置”。只要应用程序正在运行(即未挂起),并且具有足够的授权(即,它具有 WhenInUse 授权并在使用中,或者它具有 Always 授权),仍然允许访问位置。但是,该应用程序仍将遵守通常的任务暂停规则。

有关可能的授权值的更多详细信息,请参阅 -requestWhenInUseAuthorization 和 -requestAlwaysAuthorization。

回答by ElonChan

If you're using CoreLocation framework in your app in Xcode7(pre-released),and you may notice that there is a newly added property called allowsBackgroundLocationUpdates in CLLocationManager class.

如果您在 Xcode7(预发布)的应用程序中使用 CoreLocation 框架,您可能会注意到在 CLLocationManager 类中新增了一个名为 allowedBackgroundLocationUpdates 的属性。

This new property is explained in the WWDC session "What's New in Core Location". enter image description here

这个新属性在 WWDC 会议“核心位置的新特性”中进行了解释。 在此处输入图片说明

The default value is NOif you link against iOS 9.

默认值是NO如果您针对 iOS 9 进行链接。

If your app uses location in the background (without showing the blue status bar) you have to set allowsBackgroundLocationUpdatesto YESin addition to setting the background mode capability in Info.plist. Otherwise location updates are only delivered in foreground. The advantage is that you can now have location managers with background location updates and other location managers with only foreground location updates in the same app. You can also reset the value to NOto change the behavior.

如果您的应用程序在后台使用位置(不显示蓝色状态栏),除了在 Info.plist 中设置后台模式功能外,您还必须设置allowsBackgroundLocationUpdatesYES。否则位置更新仅在前台提供。优点是您现在可以在同一应用程序中拥有具有后台位置更新的位置管理器和仅具有前台位置更新的其他位置管理器。您还可以将值重置为NO以更改行为。

The documentation is pretty clear about it:

文档对此非常清楚:

By default, this is NO for applications linked against iOS 9.0 or later, regardless of minimum deployment target.

With UIBackgroundModes set to include "location" in Info.plist, you must also set this property to YES at runtime whenever calling -startUpdatingLocation with the intent to continue in the background.

Setting this property to YES when UIBackgroundModes does not include "location" is a fatal error.

Resetting this property to NO is equivalent to omitting "location" from the UIBackgroundModes value. Access to location is still permitted whenever the application is running (ie not suspended), and has sufficient authorization (ie it has WhenInUse authorization and is in use, or it has Always authorization). However, the app will still be subject to the usual task suspension rules.

See -requestWhenInUseAuthorization and -requestAlwaysAuthorization for more details on possible authorization values.

默认情况下,对于与 iOS 9.0 或更高版本链接的应用程序,无论最低部署目标如何,这都是 NO。

将 UIBackgroundModes 设置为在 Info.plist 中包含“location”,您还必须在运行时将此属性设置为 YES,无论何时调用 -startUpdatingLocation 并意图在后台继续。

当 UIBackgroundModes 不包含“位置”时,将此属性设置为 YES 是一个致命错误。

将此属性重置为 NO 等效于从 UIBackgroundModes 值中省略“位置”。只要应用程序正在运行(即未挂起),并且具有足够的授权(即,它具有 WhenInUse 授权并在使用中,或者它具有 Always 授权),仍然允许访问位置。但是,该应用程序仍将遵守通常的任务暂停规则。

有关可能的授权值的更多详细信息,请参阅 -requestWhenInUseAuthorization 和 -requestAlwaysAuthorization。

Set Info.plist like: enter image description here

将 Info.plist 设置为: 在此处输入图片说明

The syntax for the Info.plist configuration looks like this:

Info.plist 配置的语法如下所示:

<key>NSLocationAlwaysUsageDescription</key>
<string>I want to get your location Information in background</string>

<key>UIBackgroundModes</key>
<array>
    <string>location</string>
</array>

Or pull up the Capabilities tab of your app target.

或者拉出您的应用程序目标的功能选项卡。

enter image description here
(source: raywenderlich.com)

在此处输入图片说明
(来源:raywenderlich.com

Use like:

像这样使用:

_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
[_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8) {
    [_locationManager requestAlwaysAuthorization];
}
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9) {
    _locationManager.allowsBackgroundLocationUpdates = YES;
}
[_locationManager startUpdatingLocation];

I write a Demo Here (Demo2)

在这里写一个 Demo (Demo2)

回答by Manish Nahar

{
NSArray* backgroundModes  = [NSBundle MainBundle].infoDictionary[@"UIBackgroundModes"];

     if(backgroundModes && [backgroundModes containsObject:@"location"]) {
         if([manager respondsToSelector:@selector(setAllowsBackgroundLocationUpdates:)]) {
             // We now have iOS9 and the right capabilities to set this:
             [manager setAllowsBackgroundLocationUpdates:YES];
         }
     }
}

回答by Prapon Toongsi Tum Tom

I faced the same location service on background Mode for iOS 9.0.x and I fixed it as recommended in this post by adding below codes

我在 iOS 9.0.x 的后台模式下遇到了相同的位置服务,我按照这篇文章中的建议通过添加以下代码来修复它

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9) {
    _locationManager.allowsBackgroundLocationUpdates = YES;
}

However it doesn't work in iOS 9.1 Does anyone have the same problem??? If so, please kindly help. Thanks

但是它在 iOS 9.1 中不起作用有人有同样的问题吗???如果是这样,请帮助。谢谢

回答by TalL

Well, I'm still using xCode 6, since 7 beta always crashes with the simulator, and I have this problem although I don't even link against iOS9! And I can't set this property since it doesn't exist in iOS8! Oh Apple, when will the torment end?!

好吧,我仍在使用 xCode 6,因为 7 beta 总是与模拟器崩溃,尽管我什至没有链接到 iOS9,但我遇到了这个问题!而且我无法设置此属性,因为它在 iOS8 中不存在!哦,苹果,折磨什么时候结束?!

I changed it to this xCode6 compatible version by doing this late bound invocation:

我通过执行此后期绑定调用将其更改为此 xCode6 兼容版本:

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0) {

            BOOL yes = YES;

            NSMethodSignature* signature = [[CLLocationManager class] instanceMethodSignatureForSelector: @selector( setAllowsBackgroundLocationUpdates: )];
            NSInvocation* invocation = [NSInvocation invocationWithMethodSignature: signature];
            [invocation setTarget: locationManager];
            [invocation setSelector: @selector( setAllowsBackgroundLocationUpdates: ) ];
            [invocation setArgument: &yes atIndex: 2];
            [invocation invoke];
        }

Confirmed working on iOS8 (doesn't do anything) and on iOS9 beta 6 (invokes the method correctly).

确认在 iOS8(不做任何事情)和 iOS9 beta 6(正确调用方法)上工作。

回答by Tommy Woodfin

Hereis a summary of many methods that are updated from iOS 8 to iOS 9

这里总结了很多从iOS 8更新到iOS 9的方法

Many APIs and codes should be searched for each framework you are using. So search in General the framework and then find these methods to update depreciated methods.

应该为您使用的每个框架搜索许多 API 和代码。所以在General中搜索框架,然后找到这些方法来更新折旧方法。