我的应用在iOS 7上运行良好,但在iOS 8 SDK上却无法运行。
CLLocationManager不返回位置,我没有看到我的应用程序下设置->位置服务。我在这个问题上做了谷歌搜索,但没有任何结果。会有什么问题呢?
我的应用在iOS 7上运行良好,但在iOS 8 SDK上却无法运行。
CLLocationManager不返回位置,我没有看到我的应用程序下设置->位置服务。我在这个问题上做了谷歌搜索,但没有任何结果。会有什么问题呢?
当前回答
我在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中。
其他回答
在[locationManager startUpdatingLocation];之前,添加一个iOS8位置服务请求:
if([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
[locationManager requestAlwaysAuthorization];
编辑应用程序的信息。并添加键NSLocationAlwaysUsageDescription与字符串值,将显示给用户(例如,我们尽我们最大的努力来保持您的电池寿命。)
如果你的应用程序只在应用程序打开时需要位置服务,请替换:
requestAlwaysAuthorization with requestWhenInUseAuthorization和
NSLocationAlwaysUsageDescription和NSLocationWhenInUseUsageDescription。
这是ios 8的问题 将此添加到代码中
if (IS_OS_8_OR_LATER)
{
[locationmanager requestWhenInUseAuthorization];
[locationmanager requestAlwaysAuthorization];
}
到info.plist:
<key>NSLocationUsageDescription</key>
<string>I need location</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>I need location</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>I need location</string>
我也因为同样的问题而抓狂。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可能会为这些键添加默认条目,但还没有确认。
你可以在这里找到更多关于这两个设置的信息
要在iOS 8中访问用户位置,你必须添加,
NSLocationAlwaysUsageDescription in the Info.plist
这将询问用户获取其当前位置的权限。
解决方案与向后兼容,不产生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把钥匙。