如何在macOS上为CLLocationManager请求auth

时间:2022-01-28 16:55:29

I'm writing an OSX app in swift 3 that uses CLLocationManager, according to the docs and all the examples I've found the following should be fine (this is in a class that that is a CLLocationManagerDelegate)

我正在swift 3中编写一个使用CLLocationManager的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");
}

but it seems requestWhenInUseAuthorization (and requestAlwaysAuthorization) aren't available to OSX. I've currently got those function calls wrapped in #if blocks:

但似乎请求USUseseAuthorization(和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

So does anyone know how to get this working in a macOS desktop application?

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

1 个解决方案

#1


5  

According to the Core Location Best Practices WWDC 2016 session,

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

For macOS, we only support always authorization. Furthermore, Core Location will automatically display a prompt when you attempt to access location information.

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

You don't need to call requestAlwaysAuthorization on macOS.

您无需在macOS上调用requestAlwaysAuthorization。

Don't forget to turn on "Location" under the "App Sandbox" capability for your target. Also, in my tests, the prompt was only shown the first time the app was run.

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

#1


5  

According to the Core Location Best Practices WWDC 2016 session,

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

For macOS, we only support always authorization. Furthermore, Core Location will automatically display a prompt when you attempt to access location information.

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

You don't need to call requestAlwaysAuthorization on macOS.

您无需在macOS上调用requestAlwaysAuthorization。

Don't forget to turn on "Location" under the "App Sandbox" capability for your target. Also, in my tests, the prompt was only shown the first time the app was run.

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