OS X 保存位置权限

OS X save location permission

我正在这样获取用户的 GPS 位置:

var manager:CLLocationManager!

override func viewDidLoad() {
    super.viewDidLoad()

    manager = CLLocationManager()
    manager.delegate = self
    manager.desiredAccuracy = kCLLocationAccuracyBest
    manager.startUpdatingLocation()
    //manager.requestAlwaysAuthorization()
    //manager.requestWhenInUseAuthorization()

}

func locationManager(manager:CLLocationManager, didUpdateLocations locations:    [AnyObject]) { // Updated to current array syntax [AnyObject] rather than  AnyObject[]
    println("locations = \(locations)")
}

os x 应用程序似乎不存在注释掉的函数。它对我仍然有效,所以很好。

但每次我 运行 代码都会请求使用位置的权限。 是否 possible 将权限存储在某处,所以它只在第一次执行时询问?

引用 Apple 的 documentation

Call the authorizationStatus class method to get the current authorization status for your app.

If the authorization status is kCLAuthorizationStatusRestricted or kCLAuthorizationStatusDenied, your app is not permitted to use location services and you should abort your attempt to use them.

您在 OS X 上的代码应如下所示:

override func viewDidLoad() {
    super.viewDidLoad()

    let status = CLLocationManager.authorizationStatus()
    if status == .Restricted || status == .Denied {
        // User permission not given
        return
    }
    
    manager = CLLocationManager()
    manager.delegate = self
    manager.desiredAccuracy = kCLLocationAccuracyBest
    manager.startUpdatingLocation()
}

权限由 Mac OS X 集中管理。您第一次调用 startUpdatingLocation 时,它会请求权限,然后记住用户的决定。