ios中位置权限_iOS 13中的位置权限
ios中位置權(quán)限
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對位置權(quán)限的行為進行了重大更改,尤其是對于“始終允許”權(quán)限。 自3年以來我一直在全職工作于位置跟蹤應(yīng)用程序時,在支持iOS 13時遇到了巨大挑戰(zhàn)并從中學(xué)到了很多。在這個故事中,我想分享我在處理“始終允許”方面的經(jīng)驗。請求,以及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.
注意 :我假設(shè)讀者知道實現(xiàn)CoreLocation的權(quán)限和功能。 在這個故事中,我僅討論行為方面,這不是編碼教程。
權(quán)限行為摘要 (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:
在代碼中,開發(fā)人員可以請求兩種類型的權(quán)限:“使用中”( requestWhenInUseAuthorization )和“始終允許”( requestAlwaysAuthorization )。 在iOS 13中,無論使用哪種選項,我們都會顯示以下系統(tǒng)彈出窗口:
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.
如果用戶在應(yīng)用程序和iOS設(shè)置中選擇“不允許”,我們將獲得.denied權(quán)限。 這是一個簡單的案例。
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.
如果用戶選擇“允許一次”,則在應(yīng)用程序中,我們將獲得.authorizedWhenInUse權(quán)限,并在iOS設(shè)置中獲得“下次詢問”。 此選項是iOS 13中的新增功能,它取代了“始終允許”選項。 如果用戶選擇此選項,則在重新啟動應(yīng)用程序時,應(yīng)用程序和iOS設(shè)置中的權(quán)限狀態(tài)都將更改回.notDeterminated 因此我們可以再次請求位置權(quán)限。
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.
如果用戶選擇“在使用應(yīng)用程序時允許”,那么這是令人困惑的部分,因為在請求“使用時”和“始終允許”之間以及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.
如果應(yīng)用程序請求“使用中”,則此選項“允許使用應(yīng)用程序”將同時為應(yīng)用程序和系統(tǒng)設(shè)置.authorizedWhenInUse權(quán)限。 很好,因為應(yīng)用程序可以達到預(yù)期的效果。
In case app requests “Always Allow”, this choice “Allow While Using App” gives app .authorizedAlways and system setting .authorizedWhenInUse.
如果應(yīng)用程序請求“始終允許”,則此選項“在使用應(yīng)用程序時允許”將為應(yīng)用程序.authorizedAlways 和系統(tǒng)設(shè)置.authorizedWhenInUse 。
authorizedWhenInUse in app ; authorizedWhenInUse in setting (source: WWDC 2019) 在應(yīng)用中已授權(quán)WhenInUse; 設(shè)置中使用 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.
您可能會問:“為什么它們不同? 這是一個錯誤嗎?”。 好吧,你并不孤單。 這就是蘋果所謂的延期許可或臨時許可。 無論我們叫什么,這意味著真正的“始終允許”權(quán)限將在iOS系統(tǒng)決定的時間稍后顯示。
WWDC 2019)WWDC 2019 )In real life, user can expect to see the popup below a few days later:
在現(xiàn)實生活中,用戶可以期待幾天后看到以下彈出窗口:
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字符串作為消息。 如果用戶選擇“僅在使用時更改”,則應(yīng)用程序和設(shè)置都將獲得權(quán)限.authorizedWhenInUse 。 否則,如果用戶選擇“總是允許”,那么這兩個程序,現(xiàn)在設(shè)置得到了“真正的” .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中的這一新行為暗示了跟蹤應(yīng)用程序設(shè)計師的挫敗感。 由于應(yīng)用程序在開始時就獲得了“偽裝”的“始終允許”權(quán)限,因此用戶可能會在第二次確認時不小心將“更改為僅在使用時”,從而破壞了應(yīng)用程序的功能。 許多公司必須在其應(yīng)用程序中設(shè)計一個強制設(shè)置權(quán)限屏幕,以將用戶重定向到iOS設(shè)置中的位置權(quán)限,并讓用戶手動選擇正確的權(quán)限。
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似乎意識到需要改善一些用戶體驗。 因此,正如我在開始時提到的那樣,他們決定允許開發(fā)人員立即顯示第二個彈出窗口。 但是它并沒有一個簡單的實現(xiàn)。 如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
當前具有“使用中”授權(quán)且從未請求過“永遠”授權(quán)的應(yīng)用程序可以使用此方法僅一次請求“永遠”授權(quán)
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:
實際上,這意味著,對于新用戶(在應(yīng)用內(nèi)之前絕不授予任何位置許可),我們必須首先顯示“使用時”請求。 如果用戶授予“使用中”權(quán)限,則我們發(fā)出“始終允許”請求以彈出第二個請求。 您可以從以下代碼中獲得啟發(fā):
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中,我們可以設(shè)置一個閉包,因此當用戶回答第一個彈出窗口時,我們可以檢測到他們是否給出了“正確”選擇,如果是,則顯示“始終允許”請求。
Using this code, we will get the desired result:
使用此代碼,我們將獲得所需的結(jié)果:
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中位置權(quán)限
總結(jié)
以上是生活随笔為你收集整理的ios中位置权限_iOS 13中的位置权限的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: gstreamer教程及在DM3730上
- 下一篇: pyqt5-weidget-鼠标操作(鼠