【发布时间】:2020-04-22 00:57:58
【问题描述】:
长话短说:
我想让“位置”出现在应用设置中,而不像 Tile 应用那样请求位置授权。
重现步骤:
- 安装磁贴应用程序。
- 应用设置中没有“位置”。
- 打开应用,接受蓝牙访问请求(有关系吗?)。
- 应用设置中仍然没有“位置”,但出现了蓝牙。
- 使用应用程序大约 20 秒(不能更短)。
- “位置”出现在应用设置中 - 如何?
这在下面的视频中展示:
https://media.giphy.com/media/h5dPQPbBHzEhdLTrKz/giphy.gif
我怎样才能做到这一点?
背景 - 为什么
自 iOS 13 起,无法直接向用户询问Always 位置授权。当开发者请求Always授权时,用户只能选择While in Use选项,应用获得Provisional Always授权。直到再次提示用户(iOS 决定何时),用户将在应用设置中看到While in Use 授权。
这意味着:
Always--> 应用设置中的CLAuthorizationStatus.authorizedAlways和Always。Provisional Always--> 应用设置中也有CLAuthorizationStatus.authorizedAlways但While in Use。
这在this Stack Overflow answer中有很好的描述。
The problem is that the application cannot read the location in the background without Always authorization(可以,但只能持续 5-10 秒),这极大地限制了某些应用程序(例如 iBeacon 跟踪器)的主要功能。
一个众所周知的做法是检查应用程序是否具有Always 授权,如果没有,请提供说明为什么这很重要以及用户如何更改它的信息(在设置中手动)。
但是我们无法区分是Always还是Provisional Always授权状态(at least directly),所以逻辑:
if (CLLocationManager.authorizationStatus() != .authorizedAlways) {
// Prompt the user to change Location access to Always manually in settings
}
不适用于Provisional Always 授权状态。
解决办法可以是要求用户在请求位置授权之前在设置中手动选择Always,以防止出现Provisional Always状态。我认为不先打电话给requestAlwaysAuthorization() 是不可能的,但是 Tile 以某种方式做到了,如上一个视频中所述。
更新:
我已经有了:
NSLocationAlwaysAndWhenInUseUsageDescriptionNSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription
Info.plist 文件中设置的私钥。
【问题讨论】:
标签: ios core-location cllocationmanager ibeacon ios13