如何在macOS上为CLLocationManager请求auth

sam*_*ael 8 macos cllocationmanager swift

我正在使用CLLocationManager在swift 3中编写一个OSX应用程序,根据文档和我发现的所有示例应该没问题(这是一个类中的一个CLLocationManagerDelegate)

if CLLocationManager.locationServicesEnabled() {
    let lm = CLLocationManager()            
    lm.requestWhenInUseAuthorization()            
    lm.delegate = self
    lm.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
    lm.startUpdatingLocation()
    print("should start getting location data")
} else {
    print("Location service disabled");
}
Run Code Online (Sandbox Code Playgroud)

但似乎requestWhenInUseAuthorization(和requestAlwaysAuthorization)似乎不适用于OSX.我目前有这些函数调用包裹在#if块中:

#if os(macOS)
    // can't find way for MacOSX to request auth
#endif

#if os(watchOS) || os(tvOS)
    lm.requestWhenInUseAuthorization()
#endif

 #if os(iOS)
    lm.requestAlwaysAuthorization()
 #endif
Run Code Online (Sandbox Code Playgroud)

那么有谁知道如何在macOS桌面应用程序中使用它?

Mat*_*ues 10

根据WWDC 2016年核心位置最佳实践会议,

对于macOS,我们只支持始终授权.此外,当您尝试访问位置信息时,Core Location将自动显示提示.

您无需在macOS上调用requestAlwaysAuthorization.

不要忘记打开目标"App Sandbox"功能下的"Location".此外,在我的测试中,提示仅在第一次运行应用程序时显示.