Using the LocationButton in SwiftUI for One Time Location Access

With this reference, you will be able to use the LocationButton in a SwiftUI app to conveniently access the user's location data.

Using the LocationButton in SwiftUI for One Time Location Access

At WWDC 2021 Apple introduced the LocationButton. It is part of the new CoreLocationUI framework that provides access to users' location data through a standardized and secure UI. It comes with the CLLocationButton to be used with UIKit and the LocationButton for SwiftUI.

The Button is used to simplify one-time authorization requests to access location data. The first time the button is tappen, Core Location asks the user to grant temporary access to their location data. Upon consent, the app receives a temporary CLAuthorizationStatus.authorizedWhenInUse authorization.

Not only is this a super convenient way to access location data that simplifies the implementation of Core Location Managers, but it also is very much in line with privacy by design principles. So if location access is only needed once, this is the way to go.

Let's explore a simple implementation to request temporary location access using MapKit to show the user's location on the map. For this, we have to create a simplified manager to deal with location data and the configuration of MapViews.

Let's go. Become a free member or log in to proceed.