Core Location UI: New way to get one-time location

Apple has new solution for apps which need location only sometimes.

iOS 15 brings new framework called Core Location UI. It offers new system button to request location for one-time use. Its intent is to provide more streamlined experience for apps that don't always need location and users tend to pick the "Allow once" option.

In UIKit this button is called CLLocationButton and in SwiftUI just LocationButton. The idea is that you should use this button on a screen that needs location for particular feature and this button will facilitate the permissions for you.

When user taps it first time, system will show modified location dialog with options "OK" and "Not Now" (at least in my testing with first Xcode 13 beta). If user selects "OK", you can start using location in the button handler. If "Not Now" is selected then next tap of the button will once again show this dialog.

So this makes it easier for users to securely grant location without additional dialogs and you as a developer don't have to manage permissions.

The action connected to this button can immediately request location and work with it.

Basic usage

The basic usage can look like this:

let clButton = CLLocationButton(frame: .zero, primaryAction: UIAction(handler: { [unowned self] _ in

I have omitted the AutoLayout code used to setup the layout for the button. This will get you the most basic button with blue background and text "Current Location". It doesn't even have rounded corners.

CLLocationButton example


Let's customize the button a bit.

clButton.icon = .arrowFilled
clButton.label = .shareCurrentLocation
clButton.cornerRadius = 16

This looks much better.

CLLocationButton example customization

Note that I had to explicitly specify the label property which let's you show one of a few predefined titles. Otherwise with icon set, you would see just an icon which might be useful if you want to go for the circle design. You can also customize the fontSize on the CLLocationButton and then stuff like backgroundColor or tintColor for the text and icon.

Another great "feature" is that the title is automatically translated to other languages, assuming your app supports them.

However be careful because there are certain built-in rules around the button size and the contrast between background and foreground. If these aren't satisfied, you will get an error in the developer console. For example if the height is less than 44pt.

SwiftUI LocationButton

SwiftUI usage is pretty similar and there is basic sample code available in the docs. You would use the LocationButton like this:

LocationButton(.currentLocation) {
  // Fetch location with Core Location.

More resources

Sample project is available from Apple and you can also check the corresponding WWDC session.

Uses: Xcode 13 & Swift 5.5

Filip Němeček profile photo


Filip Němeček @nemecek_f

iOS blogger and developer with interest in Python/Django. Telling other devs' stories with iOS Chat.