OGeek|极客世界-中国程序员成长平台

标题: ios - iOS 8 中的 CLLocationManager 故障排除 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 09:49
标题: ios - iOS 8 中的 CLLocationManager 故障排除

根据新的更改,我确实更新了 CLLocationManager 的代码,并添加了 NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription 键。

manager = [[CLLocationManager alloc] init];
manager.delegate = self;
manager.distanceFilter = kCLDistanceFilterNone;
manager.desiredAccuracy = kCLLocationAccuracyBest;
NSLog(@"%d",[CLLocationManager authorizationStatus]); // always output 0
// check if iOS 8
if ([manager respondsToSelectorselector(requestAlwaysAuthorization)]) {
    [manager requestAlwaysAuthorization];
}
[manager startUpdatingLocation];

问题:该应用程序不再请求任何位置管理,即我在设置 -> 隐私 -> 位置服务和 locationManagerCLLocationManager *)aManager didUpdateLocationsNSArray *)locations< 下找不到它 仍然没有被触发。我有什么遗漏吗?

编辑:[CLLocationManager authorizationStatus]0kCLAuthorizationStatusNotDetermined,因此我认为问题出在 [manager requestAlwaysAuthorization ] 供引用,这是我的本地化 InfoPlist.strings (可能会导致问题):

<key>NSLocationAlwaysUsageDescription</key>
<string>Text</string>

<key>NSLocationWhenInUseUsageDescription</key>
<string>Text</string>

调用[manager requestAlwaysAuthorization]被执行,locationManager:didFailWithError:被执行但未被触发。

尝试使用不同的 Plist,但仍然没有成功:

<key>NSLocationWhenInUseUsageDescription</key>
<string>Text</string>

<key>NSLocationUsageDescription</key>
<string>Text</string>

还有一点需要注意:我的 Plist 是本地化的,这会是个问题吗?



Best Answer-推荐答案


您说您根本没有看到请求授权的警报,确认委托(delegate)方法 didChangeAuthorizationStatus 没有触发,然后您转到设置应用程序 -> 隐私 -> 位置服务,并且该程序未在此处列出并且未列出拒绝定位服务。因此,这听起来像是一个 plist 问题。

就 info.plist 而言,我是在 Xcode 中完成的(对我来说,没有语言本地化稍微简单一些)转到 info 选项卡并输入“NSLocationWhenInUseUsageDescription”(您还想添加“NSLocationAlwaysUsageDescription”)并输入说明。如果您看到弹出窗口(这是异步的),它正在工作。如果它找不到您为什么需要定位服务的描述,它不会向用户显示警报(必须显示您的描述),这当然意味着您不会获得授权。

来自关于 infoplist.strings 的苹果文档 https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/InfoPlistKeyReference.pdf :

“在 Info.plist 文件中查找键值的例程会考虑用户的语言偏好,并在存在时返回键的本地化版本(来自相应的 InfoPlist.strings 文件)。如果本地化版本的键不存在,例程返回 Info.plist 文件中存储的值。"

因此必须将这些键添加到 info.plist(可以在项目的 xcode 信息选项卡中完成),并且只有当它找到另一种语言的文件时,它才会尝试使用您的本地化值。

我会进入项目,选择目标,然后在build设置旁边的信息选项卡中,确保您看到 plist 键 NSLocationAlwaysUsageDescription 已添加和描述。添加 plist 键时,我没有从下拉菜单中找到选择,并且有一个选项,Privacy - Location Usage Description,对我有用。只需复制“NSLocationAlwaysUsageDescription”这个词并添加描述。您也可以在使用时进行。它必须在那里才能工作。它需要在查找本地 plist 键之前找到全局。对于调试,您可以从本地化 plist 中删除键以启动并验证它是否与全局 plist 值一起使用。或者更确切地说:“从 plist 中删除本地化常量,并且只有全局常量”没有理由只有全局 plist 值它不应该工作。如果您有多个翻译的语言需求,您只需要稍后添加本地化值,一旦您确认全局作品。顺序应该是让全局仅作为最佳实践首先工作,因为通过选中该步骤,您就知道第一部分是否正常工作。您不需要翻译主要 plist 描述的新项目可以添加而不是本地化。

如果您想要第二语言,请注意 https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/AboutInformationPropertyListFiles.html 中的此示例.

info.plist的格式为:

<key>CFBundleDisplayName</key>
<string>TextEdit</string>

但如果你有一个法语 infoPlist.strings 格式是:

CFBundleDisplayName = "TextEdit";

关于ios - iOS 8 中的 CLLocationManager 故障排除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26833695/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4