我的应用在iOS 7上运行良好,但在iOS 8 SDK上却无法运行。

CLLocationManager不返回位置,我没有看到我的应用程序下设置->位置服务。我在这个问题上做了谷歌搜索,但没有任何结果。会有什么问题呢?


当前回答

Swift开发者常犯的一个错误:

首先确保你添加了一个值到plist为NSLocationWhenInUseUsageDescription或NSLocationAlwaysUsageDescription。

如果你仍然没有看到一个窗口弹出要求授权,看看你是否把行var locationManager = CLLocationManager()在你的视图控制器的viewDidLoad方法。如果这样做,那么即使调用locationManager.requestWhenInUseAuthorization(),也不会显示任何内容。这是因为在viewDidLoad执行后,locationManager变量被释放(清除)。

解决方案是在类方法的顶部找到var locationManager = CLLocationManager()行。

其他回答

苹果的文件显示:

https://developer.apple.com/documentation/corelocation/requesting_permission_to_use_location_services https://developer.apple.com/documentation/corelocation/cllocationmanager/1620562-requestwheninuseauthorization

As of iOS 8, the presence of a NSLocationWhenInUseUsageDescription or a NSLocationAlwaysUsageDescription key value in your app's Info.plist file is required. It's then also necessary to request permission from the user prior to registering for location updates, either by calling [self.myLocationManager requestWhenInUseAuthorization] or [self.myLocationManager requestAlwaysAuthorization] depending on your need. The string you entered into the Info.plist will then be displayed in the ensuing dialog.

如果用户授予了权限,则业务照常进行。如果他们拒绝权限,则委托不会被告知位置更新。

在[locationManager startUpdatingLocation];之前,添加一个iOS8位置服务请求:

if([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
    [locationManager requestAlwaysAuthorization];

编辑应用程序的信息。并添加键NSLocationAlwaysUsageDescription与字符串值,将显示给用户(例如,我们尽我们最大的努力来保持您的电池寿命。)

如果你的应用程序只在应用程序打开时需要位置服务,请替换:

requestAlwaysAuthorization with requestWhenInUseAuthorization和

NSLocationAlwaysUsageDescription和NSLocationWhenInUseUsageDescription。

我也因为同样的问题而抓狂。Xcode给出错误:

试图启动MapKit位置更新而没有提示 位置授权。必须调用-[CLLocationManager requestWhenInUseAuthorization]或-[CLLocationManager . request requestAlwaysAuthorization]。

但即使实现了上述方法之一,它也不会提示用户,除非信息中有条目。NSLocationAlwaysUsageDescription或NSLocationWhenInUseUsageDescription。

添加以下行到您的信息。Plist,其中字符串值代表您需要访问用户位置的原因

<key>NSLocationWhenInUseUsageDescription</key>
<string>This application requires location services to work</string>

<key>NSLocationAlwaysUsageDescription</key>
<string>This application requires location services to work</string>

我认为自从我在Xcode 5中开始这个项目以来,这些条目可能已经丢失了。我猜Xcode 6可能会为这些键添加默认条目,但还没有确认。

你可以在这里找到更多关于这两个设置的信息

解决方案与向后兼容,不产生Xcode警告:

SEL requestSelector = NSSelectorFromString(@"requestWhenInUseAuthorization");
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined &&
  [self.locationManager respondsToSelector:requestSelector]) {
((void (*)(id, SEL))[self.locationManager methodForSelector:requestSelector])(self.locationManager, requestSelector);
  [self.locationManager startUpdatingLocation];
} else {
  [self.locationManager startUpdatingLocation];
}

在Info.plist中设置NSLocationWhenInUseUsageDescription键。

对于iOS版本11.0+: 在Info.plist中设置NSLocationAlwaysAndWhenInUseUsageDescription键。还有另外2把钥匙。

我在InfoPlist中添加了这些键。字符串在iOS 8.4, iPad mini 2。这也很有效。我没有在Info.plist中设置任何键,比如NSLocationWhenInUseUsageDescription。


InfoPlist.strings:

"NSLocationWhenInUseUsageDescription" = "I need GPS information....";

基于这个线程,它说,就像在iOS 7中一样,可以本地化到InfoPlist.strings中。在我的测试中,这些键可以直接在InfoPlist.strings文件中配置。

因此,您需要做的第一件事是将>下面的一个或两个键添加到您的Info中。plist文件: NSLocationWhenInUseUsageDescription NSLocationAlwaysUsageDescription 这两个键都有一个字符串,用来描述你为什么 需要位置服务。您可以输入一个字符串,例如“Location is” 需要知道你在哪里”,这在iOS 7中是可以做到的 在InfoPlist中本地化。字符串文件。


更新:

我认为@IOS的方法更好。将键添加到信息。将本地化的字符串添加到InfoPlist.strings中。