Get location coordinates using CLLocationManager in Swift

· 2 min read
Get location coordinates using CLLocationManager in Swift

To get the location (latitude and longitude) of a device in Swift, you can use the Core Location framework, which is an official library from Apple. Only 2 steps are required, so it is pretty simple:

Step 1: Set up proper permission

To get the location of a device in Swift, you need to request the location permission from the user. In order to do this, you need to include the NSLocationWhenInUseUsageDescription or the NSLocationAlwaysUsageDescription key in the Info.plist file of your app, depending on the level of access to the location data that you need.

For example, if you want to request the location permission only when your app is in use, you can add the following entry to the Info.plist file (P.S. If you are new to the plist format file, it is recommend for you to read this: PLIST in iOS):

<key>NSLocationWhenInUseUsageDescription</key>
<string>Your location is needed to provide the app's features.</string>

This will display a prompt to the user asking for the location permission when your app is launched or when the location services are used for the first time. If the user grants the permission, your app will be able to access the location data while it is in use.

Similarly, if you want to request the location permission always, even when your app is in the background, you can use the NSLocationAlwaysUsageDescription key instead.

Once you have included the appropriate key in the Info.plist file, you can use the Core Location framework to request and receive the location updates from the system. You can find more information and examples of how to do this in my previous answer.

Step 2: Coding part

and make your class/struct conforms to CLLocationManagerDelegate.

Here is an example of how to do this using the CLLocationManager class:

import CoreLocation

class LocationManager: NSObject, CLLocationManagerDelegate {
    private let locationManager = CLLocationManager()
    var location: CLLocation?

    override init() {
        super.init()
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        location = locations.last
    }
}

In this example, the LocationManager class is a wrapper around the CLLocationManager class that is used to request and receive the location updates from the system. The locationManager(_:didUpdateLocations:) method of the CLLocationManagerDelegate protocol is called whenever the location manager receives new location data, and it is used to update the location property of the LocationManager class with the most recent location.

To use the LocationManager class in your app, you can instantiate it and access the location property whenever you need to get the location data. You can also use the CLLocationManager class directly, if you prefer, or you can use other location services or APIs that are available on iOS.

It is important to note that getting the location of a device requires the user's permission and might involve significant resources, such as GPS, Wi-Fi, or cellular data. It is therefore important to design and implement the location feature of your app with care, and to optimize the use of the location services to minimize the impact on the battery life and the performance of the app. It is also important to handle errors and exceptions appropriately, and to consider the privacy and security implications of collecting and storing the location data.