- Handling Location Permissions in iOS 14
- Anupam Chugh
- The Nuggets translation Project
- Permanent link to this article: github.com/xitu/gold-m…
- Translator: Godlowd lsvih
- Proofread by: Lsvih, Hoarfroster
Handles location permissions and manages approximate address access in iOS 14
Apple is the undisputed leader in data privacy. Access to location has been misused or abused by different apps in the past. This kind of behavior is not only a threat to security, even a kind of destruction. With iOS 14, Apple once again wants to give users more control over the data they are sharing.
IOS 14 has made minor changes to the CoreLocation framework. Furthermore, users can choose to give access to precise or approximate geographic locations.
Before we learn how to master the new location changes in iOS 14, let’s quickly review what’s new in iOS 13’s location permissions.
IOS 13 Location Permissions quick review
Last year, Apple changed the way geolocation tracking permissions work in iOS 13.
- The most obvious change is the new “only once” permission, which requires setting
NSLocationWhenInUseUsageDescription
Configure the content. It is important to note that this permission will be revoked automatically when the application is closed. - In addition, enabling “Allow While Using The App” will temporarily “always Allow” location tracking. Now, when you try to access a location in the background, the user is presented with a dialog box to choose whether to continue to allow tracking.
- IOS 13.4 introduces a better way to quickly ensure that “always allow” permissions are granted. Just ask for it
authorizedWhenInUse
If it is granted permission, it will appearauthorizedAlways
“Is displayed.
For a more in-depth look at iOS 13 location permissions in your app, check out this article.
In the next section, we’ll look at how managing location has changed in an iOS 14 SwiftUI app.
So let’s get started.
IOS 14 CoreLocation framework changes
Apple has deprecated the class method authorizationStatus() that we called in CLLocationManager.
This means that starting with iOS 14, authorizationStatus() can only be called by instances of CLLocationManager.
Apple also scrapped the CoreLocation method with a status parameter in didChangeAuthorization. Instead, now has a new locationManagerDidChangeAuthorization method.
To ensure the accuracy status of the geographic location, we can use the new enumeration attribute accuracyAuthorization in the instance of the geographic location manager. This attribute is of CLAccuracyAuthorization type and has two enumerated values:
fullAccuracy
reducedAccuracy
(Returns an approximate location rather than an exact location)
CoreLocation is set exactly the same way as in iOS 13:
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
locationManager.allowsBackgroundLocationUpdates = true
locationManager.pausesLocationUpdatesAutomatically = false
Copy the code
Please note: if you need allowsBackgroundLocationUpdates, please make sure that you are in the Xcode project Settings enabled the Background model (Background mode).
Now, when you run this code on your device, on iOS 14 you’ll get the following prompt:
By pressing the precision button, you can choose to allow vague or precise geolocation permissions.
Now, it’s possible that all you need to do is access the user’s exact geographic location.
Thankfully, in iOS 14 there’s a new way to temporarily make requests:
This requestTemporaryFullAccuracyAuthorization method need a purpose keys to explain why need access to the user’s exact location. This key is defined in the info. The file in a NSLocationTemporaryUsageDescriptionDictionary dictionary, as shown in the figure below:
Once TemporaryFullAccuracyAuthorization is called, will appear the following message:
ReducedAccuracy and fullAccuracy location updates were received in the proxy method didUpdateLocations.
The source code for a demo of the iOS 14 CoreLocation app written in the Swift UI is available on GitHub.
Please be sure to note that if you use reducedAccuracy for background geographic location update, the update interval will not change. In this case, semaphores and area detection will also be disabled.
CoreLocation updates in AppClips, Widgets, and default Settings
AppClips is like a mini APP module that can run without installing a full APP.
- When you access a location via AppClips, instead of a “only allowed when using the App” option, there will be a “in-use until tomorrow” option that automatically resets at the end of the day.
- To access a geographic location in WidgetKit, you need to use the
info.plist
File definitionNSWidgetWantsLocation
The key. - If you want to display the prompt only when accessing the exact location by default, you can use the
info.plist
Add keys to filesNSLocationDefaultAccuracyReduced
. By doing so, the precise location access button is not displayed in the permissions dialog. But users can still enable toggling in the phone’s Settings.
conclusion
The CoreLocation framework has had an interesting change in iOS 14 that gives users more control over their location data. Not all apps need to obtain accurate geographic location information, so you can choose to use the reducedAccuracy attribute to only obtain approximate geographic location.
That’s all for this article, thanks for reading
If you find any mistakes in your translation or other areas that need to be improved, you are welcome to the Nuggets Translation Program to revise and PR your translation, and you can also get the corresponding reward points. The permanent link to this article at the beginning of this article is the MarkDown link to this article on GitHub.
The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. The content covers Android, iOS, front-end, back-end, blockchain, products, design, artificial intelligence and other fields. If you want to see more high-quality translation, please continue to pay attention to the Translation plan of Digging Gold, the official Weibo, Zhihu column.