如何在 macOS 上为 CLLocationManager 请求授权
how to request auth for CLLocationManager on macOS
我正在 swift 3 中编写一个使用 CLLocationManager 的 OSX 应用程序,根据 docs 和我发现以下所有示例应该没问题(这是在class那是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 session,
For macOS, we only support always authorization. Furthermore, Core
Location will automatically display a prompt when you attempt to
access location information.
You don't need to call requestAlwaysAuthorization on macOS.
不要忘记在目标的 "App Sandbox" 功能下打开 "Location"。此外,在我的测试中,提示仅在应用程序第一次显示为 运行.
时显示
在 OSX
中触发身份验证请求的步骤
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 Would like to use your current location' 弹出窗口现在会出现
我正在 swift 3 中编写一个使用 CLLocationManager 的 OSX 应用程序,根据 docs 和我发现以下所有示例应该没问题(这是在class那是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 session,
For macOS, we only support always authorization. Furthermore, Core Location will automatically display a prompt when you attempt to access location information.
You don't need to call requestAlwaysAuthorization on macOS.
不要忘记在目标的 "App Sandbox" 功能下打开 "Location"。此外,在我的测试中,提示仅在应用程序第一次显示为 运行.
时显示在 OSX
中触发身份验证请求的步骤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 Would like to use your current location' 弹出窗口现在会出现