无法从 CLLocationManager 获取权限提示



这是我从实现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和模拟器。我遵循了此处的建议:请求始终授权未显示权限警报:"将核心位置框架添加到项目设置/目标/功能/后台模式设置"位置更新"和"使用蓝牙 LE 配件"在 Info.plist NSLocationAlwaysUsageDescription"中添加密钥"。

我也试图清理和重建,没有任何变化。我感到毫无头绪。

编辑:这个问题似乎相关:iOS:应用程序在安装应用程序时没有征求用户的许可。 每次都获得kCLAuthorizationStatusNotDetermined - Objective-c和Swift,但所选答案及其文章没有暴露任何新内容

您的CLLocationManager对象是本地对象,因此在超出范围后将立即释放。使其成为类属性,然后异步进程(如请求授权和确定位置)将有机会运行。

最新更新