Location Data and Swift

With the Bike Kollective iOS app, getting location data will be fundamental in the app’s ability to allow users to find nearby bikes. Given that I’m currently putting together the App’s View that displays a list of bikes within a certain location, this functionality is vital in nailing down ASAP.

Luckily, Apple provides an iOS API that handles just that – Core Location. According to Apple documentation, Core Location not only provides the geographic location of the device, but it can also provide the orientation. While orientation might be useful for an app in which reflecting the direction the user is facing is useful, it’s not something that is relevant for the Bike Kollective. All we need to do is get the user’s location at this point.

Setting up Core Location

The first thing we need to do is import Core Location to the View Controller that it will be used with. In this case, that is the ListView.swift file. We’ll do that with a simple import statement:

import CoreLocation

Next, according to the Apple Documentation, we will need to declare an instance of CLLocationManager. CLLocationManager is a class whose instances tracking the user’s location to a configurable degree of accuracy. We’ll do that as so:

var locationManager : CLLocationManager?

We also need to link a delegate for this instance to interact with called CLLocationManagerDelegate. In this case, it will be our ListView.swift View Controller. Thus, we will need to add the delegate to the View Controller class:

class ViewController: UIViewController, CLLocationManagerDelegate  

Finally, in our viewDidLoad() method, we will call our location manager, attach the delegate to self (which in this case in the View Controller), and call the method requestWhenInUseAuthorization(). We can see this here:

override func viewDidLoad() { 
super.viewDidLoad() 
locationManager = CLLocationManager() 
locationManager?.delegate = self 
locationManager?.requestWhenInUseAuthorization() 
 }

Authorizing Location Services

Even though the above sets everything up for us to utilize Core Location, we still cannot simply access that data. The final method I mentioned above, requestWhenInUseAuthorization(), is the real sticking point. As the Apple Documentation mentions, iOS apps can request location services in two forms: Always and When In Use.

For the purposes of Bike Kollective, I believe When In Use suits our purposes, as the two reasons to use Always – automated tasks and regular location updates over time – are not within the guidelines of the app.

Additionally, according to this developer, we will need to update the info.plist file, which handles permissions, to provide a reasoning for accessing the user’s location data. This will fall under the property, NSLocationWhenInUseUsageDescription, which is listed in the file under the category, “Privacy – Location When In Use Description”.

It’s at this point that we call the locationManagerDidChangeAuthorization() function. This function, per the documentation, runs only when the user changes the status of the location services authorization. It is here where we can finally request and store the user’s location:

func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) { 
switch manager.authorizationStatus { 
case .authorizedAlways , .authorizedWhenInUse: 
// code goes here
default: return 
} 
}

It is here where we finally call the function that gets the user’s location, startUpdatingLocation(). Per the documentation, this function periodically updated the app on the user’s location. We then call the instance property of CLLocationManager storing the user’s most recently retrieved location, location, as seen here:

manager.startUpdatingLocation()
guard let currentLocation = manager.location else { return }

With that all out of the way, we finally have the user’s location stored as a CLLocation, which is Apple’s name for a latitude-longitude coordinate.

Finally, we have what we need. As I delve deeper into the app, I may want to use something like CLGeocoder, which decodes the coordinate into something more user friendly, but for now, I think I can call this mission accomplished.

Leave a comment

Your email address will not be published. Required fields are marked *