【问题标题】:Can't Determine When User Doesn't Allow Location Services无法确定用户何时不允许定位服务
【发布时间】:2015-05-08 11:42:20
【问题描述】:

我要求用户开启定位服务。我想知道用户何时点击Don't Allow,以便我可以处理一些通知。但是,当我单击Don't Allow 时,不会调用didFailWithErrordidChangeAuthorizationStatus 方法。我知道记录器中没有打印任何内容。我附上了一个代码示例。我做错了什么以及如何解决这个问题。谢谢。

import UIKit

import CoreLocation

class AwesomeViewController: UIViewController, CLLocationManagerDelegate {

    let locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        let authorizationStatus = CLLocationManager.authorizationStatus()

        if(authorizationStatus == .AuthorizedWhenInUse || authorizationStatus == .AuthorizedAlways) {
            // authorization is good
        } else {
            locationManager.requestWhenInUseAuthorization()
        }
    }


    func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        print(status)
    }

    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {            
        print(error.localizedDescription)
    }
}

【问题讨论】:

    标签: ios swift core-location cllocationmanager


    【解决方案1】:

    didFailWithErrordidChangeAuthorizationStatus 方法未被调用

    这些是委托方法。您的位置管理器没有任何委托 - 当然它没有 you(AwesomeViewController 实例)作为委托。所以它永远不会调用这些方法。您需要设置位置管理器的委托(在这种情况下,您需要将其设置为self)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多