ios中位置权限_iOS 13中的位置权限

ios中位置权限

In iOS 13, Apple made a big changes in location permission’s behaviour, especially for “Always Allow” permission. As I have been working full time on a location tracking app since 3 years, I encountered a big challenge and learned a lot from it when supporting iOS 13. In this story, I would like to share my experience when dealing with “Always Allow” request, and how iOS 13.4 brought a big improvement for user experience (though it looks like a workaround) to this request.

在iOS 13中,Apple对位置权限的行为进行了重大更改,尤其是对于“始终允许”权限。 自3年以来我一直在全职工作于位置跟踪应用程序时,在支持iOS 13时遇到了巨大挑战并从中学到了很多。在这个故事中,我想分享我在处理“始终允许”方面的经验。请求,以及iOS 13.4如何为该请求带来了极大的用户体验改善(尽管这似乎是一种解决方法)。

Note: I assume that audience knows implementing CoreLocation’s permission and functionality. In this story, I discuss only behaviour aspect, this is not a coding tutorial.

注意 :我假设读者知道实现CoreLocation的权限和功能。 在这个故事中,我仅讨论行为方面,这不是编码教程。

权限行为摘要 (Summary of permission’s behaviour)

In the code, developers can request two types of permission: “When in use” (requestWhenInUseAuthorization) and “Always Allow” (requestAlwaysAuthorization). In iOS 13, whichever option we use, we get this system popup:

在代码中,开发人员可以请求两种类型的权限:“使用中”( requestWhenInUseAuthorization )和“始终允许”( requestAlwaysAuthorization )。 在iOS 13中,无论使用哪种选项,我们都会显示以下系统弹出窗口:

Image for post

This popup uses NSLocationWhenInUseUsageDescription string in the message. As you can see, even you request for “Always Allow”, this option is not available to choose anymore.

该弹出NSLocationWhenInUseUsageDescription在消息中使用NSLocationWhenInUseUsageDescription字符串。 如您所见,即使您请求“始终允许”,此选项也不再可供选择。

If user selects “Don’t Allow”, in the app and in the iOS Setting, we get .denied permission. This is a straightforward case to handle.

如果用户在应用程序和iOS设置中选择“不允许”,我们将获得.denied权限。 这是一个简单的案例。

If user selects “Allow Once”, in the app we received .authorizedWhenInUse permission, and “Ask next time” in the iOS Setting. This option is new in iOS 13, and it replace the “Always Allow” option. If user selects this option, when restarting the app, both in app’s and iOS Setting’s permission status change back to .notDeterminated so we can request again the location permission.

如果用户选择“允许一次”,则在应用程序中,我们将获得.authorizedWhenInUse权限,并在iOS设置中获得“下次询问”。 此选项是iOS 13中的新增功能,它取代了“始终允许”选项。 如果用户选择此选项,则在重新启动应用程序时,应用程序和iOS设置中的权限状态都将更改回.notDeterminated 因此我们可以再次请求位置权限。

If user selects “Allow While Using App”, well, this is the confusing part since the behaviour is different between requesting “When in Use” and “Always Allow”, and between iOS versions.

如果用户选择“在使用应用程序时允许”,那么这是令人困惑的部分,因为在请求“使用时”和“始终允许”之间以及iOS版本之间的行为是不同的。

In case app requests “When in Use”, this choice “Allow While Using App” gives both app and system setting the .authorizedWhenInUse permission. It’s good since app get what it’s supposed to get.

如果应用程序请求“使用中”,则此选项“允许使用应用程序”将同时为应用程序和系统设置.authorizedWhenInUse权限。 很好,因为应用程序可以达到预期的效果。

In case app requests “Always Allow”, this choice “Allow While Using App” gives app .authorizedAlways and system setting .authorizedWhenInUse.

如果应用程序请求“始终允许”,则此选项“在使用应用程序时允许”将为应用程序.authorizedAlways 和系统设置.authorizedWhenInUse

Image for post
authorizedWhenInUse in app ; authorizedWhenInUse in setting (source: WWDC 2019 )
在应用中已授权WhenInUse; 设置中使用 whenWhenUse (来源: WWDC 2019 )

You may ask “Why are they different? Is it a bug?”. Well, you’re not alone. This is what Apple called deferred permission, or provisional permission. Whatever we call it, it means the real “Always Allow” permission will be shown later, at the time decided by iOS system.

您可能会问:“为什么它们不同? 这是一个错误吗?”。 好吧,你并不孤单。 这就是苹果所谓的延期许可或临时许可。 无论我们叫什么,这意味着真正的“始终允许”权限将在iOS系统决定的时间稍后显示。

Image for post
WWDC 2019) WWDC 2019 )

In real life, user can expect to see the popup below a few days later:

在现实生活中,用户可以期待几天后看到以下弹出窗口:

Image for post

Actually, this is the real popup for “Always Allow”, which uses your NSLocationAlwaysAndWhenInUseUsageDescription string as message. If user selects “Change to Only While Using”, both app and setting get the permission .authorizedWhenInUse. Otherwise, if user selects “Always Allow”, then both app and setting now get the “true” .authorizedAlways.

实际上,这是“始终允许”的真正弹出窗口,它使用您的NSLocationAlwaysAndWhenInUseUsageDescription字符串作为消息。 如果用户选择“仅在使用时更改”,则应用程序和设置都将获得权限.authorizedWhenInUse 。 否则,如果用户选择“总是允许”,那么这两个程序,现在设置得到了“真正的” .authorizedAlway s

This new behaviour in iOS 13 imply frustration for tracking app’s designers. Since app get the “pretended” Always Allow permission at the beginning, user may accidentally “Change to Only While Using” on second confirmation, and disrupt app’s functionality. Many companies had to design a force setting permission screen in their apps to redirect user to location permission in iOS Setting and let users manually choose the correct one.

iOS 13中的这一新行为暗示了跟踪应用程序设计师的挫败感。 由于应用程序在开始时就获得了“伪装”的“始终允许”权限,因此用户可能会在第二次确认时不小心将“更改为仅在使用时”,从而破坏了应用程序的功能。 许多公司必须在其应用程序中设计一个强制设置权限屏幕,以将用户重定向到iOS设置中的位置权限,并让用户手动选择正确的权限。

iOS 13.4的改进 (Improvement for iOS 13.4)

Since iOS 13.4, it seems like Apple realized that it need some user experience improvement. So, as I mentioned at the beginning, they decided to allow developer to show the second popup immediately. But it doesn’t come with a straightforward implementation. As stated in requestAlwaysAuthorization documentation in Xcode 11.4:

从iOS 13.4开始,Apple似乎意识到需要改善一些用户体验。 因此,正如我在开始时提到的那样,他们决定允许开发人员立即显示第二个弹出窗口。 但是它并没有一个简单的实现。 如Xcode 11.4中的requestAlwaysAuthorization文档中所述:

An application which currently has “when-in-use” authorization and has never before requested “always” authorization may use this method to request “always” authorization one time only

当前具有“使用中”授权且从未请求过“永远”授权的应用程序可以使用此方法仅一次请求“永远”授权

In fact, this means, for a fresh user (never gives any location permission before in app), we must show firstly a “When in Use” request. If user gives “When in Use” permission, then we make the “Always Allow” request to pop the second one. You can inspire from the code below:

实际上,这意味着,对于新用户(在应用内之前绝不授予任何位置许可),我们必须首先显示“使用时”请求。 如果用户授予“使用中”权限,则我们发出“始终允许”请求以弹出第二个请求。 您可以从以下代码中获得启发:

Simple, right? In iOS 13.4, we can just setup a closure, so when user answer the first popup, we can detect if they give the “right” choice and if so, show the “Always Allow” request.

简单吧? 在iOS 13.4中,我们可以设置一个闭包,因此当用户回答第一个弹出窗口时,我们可以检测到他们是否给出了“正确”选择,如果是,则显示“始终允许”请求。

Using this code, we will get the desired result:

使用此代码,我们将获得所需的结果:

Image for post
Happy path’s sequence when running this code on iOS 13.4.
在iOS 13.4上运行此代码时,幸福路径的顺序。

然后去哪儿? (Where to go from here?)

If you want to understand deeper in Core Location changes in iOS 13, I suggest the excellent presentation from WWDC 2019: What’s New in Core Location

如果您想更深入地了解iOS 13中的核心位置更改,我建议WWDC 2019的精彩演讲: 核心位置的新功能

翻译自: https://medium.com/swlh/location-permission-in-ios-13-f9e10917c05e

ios中位置权限

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值