Core Location - Setting up Core Location with UIKit
Tags: ios, swift, tutorial
Published: 2020-08-16 07:36:12

Exordium

Core Location is Apple’s native framework for obtaining a device’s location, altitude, and orientation. Core Location is also how a device interacts with iBeacon devices. All of this is very cool technology and can be utilized in amazing ways. For example, I have used Core Location to create a pothole reporting app, RoadHazard. I really view the mobile device as a way to integrate technology with day to day human life. 

Core Location collects data via onboard components such as Wi-Fi, GPS, Bluetooth, magnetometer, barometer, and cellular hardware. In this post, I'll show you how to initially set up Core Location in your iOS app by using a simple Storyboard-based user interface, the CLLocationManager and location simulation.

You can find the code to this project on GitHub: https://github.com/andrew-lundy/core-location-tutorial/tree/set-up

Environment: Xcode 12, Swift 5, Deployment Target: iOS 13. (Read the blog post with photos: https://rustynailsoftware.com/dev-blog/core-location-setting-up-core-location-with-uikit)

The Project

I’ve created a user interface that will show some location data using a UILabel. The location services will be enabled and obtained when the green button is pressed.

Before you can write any Core Location code, you need to provide a reason as to why your app needs the user's location in the Info.plist file. As of iOS 13, two properties are required when requesting location access to an app - NSLocationAlwaysAndWhenInUseUsageDescription and NSLocationWhenInUseUsageDescription. The names of the keys in the Info.plist file are Privacy - Location Always and When In Use Usage Description and Privacy - Location When In Use Usage Description.

Now that you’ve provided a reason to access the device location, let’s start using Core Location. In the ViewController class, you will need to import the CoreLocation framework, add a variable that holds a CLLocationManager, and extend the ViewController to be the CLLocationManagerDelegate. The CLLocationManager is the object you’ll use to request location access, start and stop location events, set the desired accuracy, and more.

Next, go ahead and instantiate the CLLocationManager and set the delegate to the ViewController. 

Before actually using any Core Location functionality, making sure the device location services are enabled is the first thing you should do. Please note that this isn’t mandatory, but it’s good to check if your users have enabled location services so you can handle the situation accordingly. I’ve added this check to the changeLocationBttnTapped method since that’s where we’ll be launching the location services.


After checking to make sure the location services are enabled, the next thing to do is to request authorization. In this case, we will be requesting 'when in use' authorization. This is exactly what it sounds like - this requests the user’s permission to use location services while the app is in use. 

If you run the app, an alert will pop up and ask you if you want to give the app access to the device’s location.


After you choose the authorization status, you need to call the startUpdatingLocation method on the locationManager in order to begin reporting the user's location. I do this within the didChangeAuthorization method of the CLLocationManagerDelegate because an app can't report location data if it's not authorized to use the device's location services. The didChangeAuthorization method is called when the app creates the location manager and any time the authorization status of the CLLocationManager changes. 

The next thing to do is to check the authorizationStatus of the CLLocationManager using a switch statement. In this example, we will only be comparing the switch value to the .authorizedWhenInUse case and simply 'returning' during the default case.


I’ve updated the UI to display the coordinate value of the device’s location. The coordinate type is a CLLocationCoordinate2D, and it holds the latitude and longitude of the location. In a future post, I will go through Geocoding with CLGeocoder class. Geocoding is the process of transforming the latitude and longitude into a human-readable address.


As for now, that’s how you set up Core Location. It’s a pretty simple process, and the framework provides powerful features that can be used in a variety of different applications.

Thank you for reading - I’ll see you in the next post!