macOS 工程开启定位服务
macOS 的定位服务不同于 iOS,不需要添加 Privacy 描述,也无需进行 requestAlwaysAuthorization()
或者 requestWhenInUseAuthorization()
。
而是在 .entitlements 文件中添加一条 Location 属性并设置成 YES,如图:
或者在「TARGETS - Singin & Capabilities」中找到 App Sandbox,勾选 Location,如图:
两者为同一操作,完成其中一个操作,另一个操作会自动完成。
之后就能在「Mac 设置 - 隐私与安全性 - 定位服务」中找到对应的应用并授权。
参考:
According to the 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.
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.
This error message means that the application has not been granted permission to obtain the user’s location. To fix this you need to ensure that the application is properly “sandboxed” and that the location is enabled. You can do this in Xcode with the following steps:
In the project navigator, click on your application’s target. This should bring up a view with tabs such as “General”, “Capabilities”, “Resource Tags”, etc.
Click on the “Capabilities” tab. This will give you a list of items such as “App Groups”, “App Sandbox” and so on. Each item will have an “On/Off” button.
Turn on the “App Sandbox” item and press the “>” button on the left to show the sandbox stuff.
In the “App Data” section, select “Location”.
Now the next time you run your application, it will ask the user to allow it to use the current location. Assuming they answer in the affirmative, you should no longer see the error but instead should see the current position highlighted on the map.