这是我在实现 CLLocationManagerDelegate 的 ViewController 中的代码:
func startLocationManager() {
let locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
println("I'm called")
locationManager.requestAlwaysAuthorization()
// locationManager.startMonitoringSignificantLocationChanges()
locationManager.startUpdatingLocation()
let status = CLLocationManager.authorizationStatus()
println(status.rawValue) // This print 0 which stands for kCLAuthorizationStatusNotDetermined
println(CLLocationManager.locationServicesEnabled()) // true
}
func locationManager(manager: CLLocationManager!,
didUpdateLocations locations: [AnyObject]!) {
println("nobody call me ever, and I'm sad")
}
出于某种原因,我从来没有得到提示/更改来自动更新位置。我已经在我的设备 iOS 8.1 和 simulartor 上试过了。我遵循了此处的建议:requestAlwaysAuthorization not showing permission alert : “将核心位置框架添加到项目设置/目标/功能/后台模式设置“位置更新”和“使用蓝牙 LE 附件”在 Info.plist NSLocationAlwaysUsageDescription 添加 key 。
我也试过清理和重建,没有任何改变。我觉得很无能。
编辑:这个问题似乎相关:iOS: App is not asking user's permission while installing the app. getting kCLAuthorizationStatusNotDetermined every time - Objective-c & Swift但所选答案及其文章没有暴露任何新内容
最佳答案
您的 CLLocationManager
对象是本地对象,因此在超出范围后将立即释放。将其设为类属性,然后请求授权和确定位置等异步过程将有机会运行。
关于ios - 无法获取 CLLocationManager 的权限提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27606865/