【发布时间】:2014-11-05 00:13:28
【问题描述】:
问题:
出于某种原因,我正在开发的应用程序没有请求使用位置服务的权限。这以前可以工作。此外,我创建了一个新项目,在该项目中我完全按照下面描述的步骤进行操作,并立即生效。
一个区别是我知道我正在开发的主应用程序中的授权状态是 .Denied,而在测试项目中是 .NotDetermined。
我在堆栈溢出时阅读了这个答案:
Core Location not requesting user permission ' 知道这一点的人不多,但是在您卸载应用程序后,该应用程序的文档和首选项仍然存储在设备上,这里:
/var/mobile/Library/Safe Harbor/myappidentifier/Container/ 在我看来,这不是苹果的明智之举,因为这可能会带来安全风险,正如您在上面所解释的那样。
如果重新安装应用程序,iOS 会自动将这些首选项复制回相应的文件夹中。这就是你所看到的行为的原因。 ' 这可以解释它,因为我确实重新安装了应用程序,但它没有说明如何解决它。
谢谢
这是我在 App 中编写的相关代码:
我在 info.plist 中设置了 NSLocationAlwaysUsageDescription 键
我导入了核心位置:
import CoreLocation
实现了正确的协议:
CLLocationManagerDelegate
设置此属性:
var locationManager = CLLocationManager()
在 ViewDidLoad 中设置委托
locationManager.delegate = self
并写了一个测试函数来测试这个:
if CLLocationManager.locationServicesEnabled() {
println("Location Services Enabled")
if CLLocationManager.authorizationStatus() == .Authorized {
println("Location Services Authorized")
} else if CLLocationManager.authorizationStatus() == .AuthorizedWhenInUse {
println("Location Services Authorized WHEN IN USE")
} else if CLLocationManager.authorizationStatus() == .NotDetermined {
println("Location Services NOT Authorized: NOT DETERMINED")
} else if CLLocationManager.authorizationStatus() == .Restricted {
println("Location Services NOT Authorized: RESTRICTED")
} else if CLLocationManager.authorizationStatus() == .Denied {
println("Location Services NOT Authorized: DENIED")
} else {
println("Location Services NOT Authorized")
locationManager.requestAlwaysAuthorization()
}
} else {
println("!!! Location Services NOT Enabled !!!")
}
控制台输出:
Location Services Enabled
Location Services NOT Authorized
【问题讨论】:
-
核心位置只询问一次。之后它就再也不问了。你不能发布需要核心位置的应用程序,如果用户拒绝,你也不能继续要求它。一旦禁用,用户必须进入设置才能激活它——但如果你弹出一条警告消息告诉他们这样做,你的应用将被应用商店审查团队拒绝。必须以不显眼的方式完成(例如,用户点击后显示警报的错误图标)
-
我的意图不是在许可被拒绝后请求许可。该应用程序从未请求许可。我刚刚删除并重新安装了它
-
在任何 Apple 平台上卸载应用程序都不会重置您的设置。只有 Microsoft 平台可以这样做。如果它在以前的安装中请求许可,它会记住这一点。
标签: ios swift core-location