我正在使用CLLocationManager在swift 3中编写一个OSX应用程序,根据docs和我发现的所有示例应该没问题(这是一个类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");
}
但似乎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
那么有谁知道如何在macOS桌面应用程序中使用它?
根据Core Location Best Practices WWDC 2016年会议,
对于macOS,我们只支持始终授权。此外,当您尝试访问位置信息时,Core Location将自动显示提示。
您无需在macOS上调用requestAlwaysAuthorization。
不要忘记打开目标“App Sandbox”功能下的“Location”。此外,在我的测试中,提示仅在第一次运行应用程序时显示。
在OSX中触发auth请求的步骤
1)使用委托设置位置管理器
manager = CLLocationManager()
manager.delegate = self
2)实现所需的委托方法(这些标记为可选,但它们不是!)
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
//Do Stuff
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
//Error
}
3)请求位置
manager.requestLocation()
就这样。现在将出现'XXX想要使用您当前位置'的弹出窗口