MacKuba

Kuba Suder's blog on Mac & iOS development

Location

App ClipsAppKitExtensionsFoundationLocationMacMapsPhotosPrivacySafariSwiftSwiftUIUIKitWatchKitWWDC 19WWDC 20

View as index

What's new in location

Categories: Location, UIKit, WWDC 20 0 comments Watch the video

Location authorization

Question: how much information does this app need to know about me to do its job?

When you think about it, there’s a pretty wide range of apps that don’t actually need to know precisely where you are, just the approximate area

Precise location is definitely needed for e.g. navigation apps, but not necessarily for social media apps

In iOS 14, when the user is asked for location access, the popup that appears shows a map with current position and a “Precise” switch

This is controlled by the user, you just get either more precise or less precise location

CLLocationManager.authorizationStatus is now an instance property, not a class method

New property accuracyAuthorization: CLAccuracyAuthorization == fullAccuracy / reducedAccuracy

locationManager(_: didChangeAuthorization status:) method is deprecated, replaced with: locationManagerDidChangeAuthorization(_:)

  • invoked when either authorization status or accuracy changes
  • inside, you need to check both manager.authorizationStatus and manager.accuracyAuthorization if needed

Location updates in reduced accuracy mode are still delivered to didUpdateLocations method

  • horizontalAccuracy tells you exactly how precise the location is
  • reduced precision location will change around 4 times an hour

If possible, build your app in such a way that it’s possible to use it with reduced accuracy if the user prefers that

Do not treat imprecise location as the true location of the user

Depending on the type of app and what you use the location info for, you may want to display approximate location differently

In Maps, if you only grant approximate location, it shows your location as a large circle + shows a warning bubble at the top “Precise Location: Off”

Asking for full precision authorization

If you have a feature that requires full accuracy, you can ask the user to temporarily grant you full location precision:

manager.requestTemporaryFullAccuracyAuthorization(withPurposeKey: “WantsToNavigate”) { … }

You have to explain the user why exactly you need precise location in this specific case (there might be different reasons), so you need to pass a specific purpose key

Purpose keys are defined in Info.plist, under NSLocationTemporaryUsageDescriptionDictionary

Temporary precise location authorization works more or less like “Allow Once”, persists until the app is relaunched

Reduced accuracy should not affect apps using the “Significant location changes” API

For apps using the “Visits” API you will still get notifications at the exact time when the user enters/leaves an area

Beacons and other region monitoring APIs are disabled in reduced accuracy

If you only need reduced accuracy, you can set locationManager.desiredAccuracy = kCLLocationAccuracyReduced to ask for less precise data

Or, set the NSLocationDefaultAccuracyReduced key in Info.plist  ⭢  then the user doesn’t even see the precision option when asked

How it works

The way it works under the hood is that location is “quantized” into sectors roughly a few kilometers in size (smaller in denser urban areas); the location does not continuously update as you move, only when you move to a different sector

The semantics of the approximate location data is that it’s supposed to answer what the user would expect to hear when they ask “Where am I?”

It takes into account things like where one city ends and another starts, or where country borders are

The true location of the user will not necessarily be near the center of the returned area, but it should usually be contained somewhere within the area

The new API is available on iOS & watchOS

All temporary access authorizations are also shared between iOS & watchOS

Change in when in use / always authorization (in 13.4): you can ask for “Always” authorization when the app is in the foreground, after you’ve been granted “While Using” authorization

App clips & widgets

Differences in location authorization for app clips:

  • they can’t get “Always” authorization
  • “While Using” is always granted until tomorrow

Differences in location authorization for widgets:

  • if a widget requires location, it needs to include the NSWidgetWantsLocation key in Info.plist
  • a widget can’t show authorization prompts – it needs to be authorized in the parent app