Requesting Permission in Core Location Tutorial

On iOS The user's location is kept private, so to use it there needs to be asked for permission. In this tutorial the steps required to ask for permission will be described. This tutorial is made with Xcode 8 Beta 6 and built for iOS 10.

Open Xcode and create a new Single View Application.

Choose Next. For product name, use IOS10RequestingPermissionTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and make sure only iPhone is selected in Devices.

Go to the Storyboard. Drag a button to the main view. Double-click the button and change the title to "Get Location".  The Storyboard will look like this.

Open the Assistant Editor and make sure the ViewController.swift file is visible. Ctrl and drag from the Button to the ViewController class to create the following Action.

Go to the ViewController.swift file and add the following line to import the CoreLocation framework.

import CoreLocation

Conform the ViewController class to the CLLocationManagerDelegate protocol. Change the class declaration line to

class ViewController: UIViewController, CLLocationManagerDelegate {}

Add the following property

let locationMgr = CLLocationManager()

The  CLLocationManager is the object that will give you the GPS coordinates. Next, implement the getMyLocation method.

@IBAction func getLocation() {
   // 1 
   let status  = CLLocationManager.authorizationStatus()
   
   // 2
   if status == .notDetermined {
       locationMgr.requestWhenInUseAuthorization()
       return
   }
    
   // 3
   if status == .denied || status == .restricted {
        let alert = UIAlertController(title: "Location Services Disabled", message: "Please enable Location Services in Settings", preferredStyle: .alert)
        
        let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
        alert.addAction(okAction)
        
        present(alert, animated: true, completion: nil)
        return
   }
   
  // 4
   locationMgr.delegate = self
   locationMgr.startUpdatingLocation()
}
  1. The authorizationStatus returns the current authorisation status.
  2. The when in use authorisation get location updates while the app is in the foreground
  3. When location services is disabled, the user will be shown an alert
  4. Send location updates to the delegate, which is the View Controller. 

Next, implement the CLLocationManager delegate methods.

// 1
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let currentLocation = locations.last!
    print("Current location: \(currentLocation)")
}

// 2
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
    print("Error \(error)")
}
  1. The current location is printed to the console.
  2. An Error is generated and displayed when the location can't get updated.

To enable the permissions for location updates while the app is running a special key is needed. Open info.plist. Right-click and select Add Row. Enter the following Values.

Build and Run the project, When the app ask for permission 

Turn on the Location Arrow in the console and select a predefined location. The current location gps coordinates will be displayed on the console.

Current location: <+51.50998000,-0.13370000> +/- 5.00m (speed -1.00 mps / course -1.00) @ 9/12/16, 3:25:27 PM Central European Summer Time
Current location: <+51.50998000,-0.13370000> +/- 5.00m (speed -1.00 mps / course -1.00) @ 9/12/16, 3:25:28 PM Central European Summer Time
Current location: <+51.50998000,-0.13370000> +/- 5.00m (speed -1.00 mps / course -1.00) @ 9/12/16, 3:25:29 PM Central European Summer Time
Current location: <+51.50998000,-0.13370000> +/- 5.00m (speed -1.00 mps / course -1.00) @ 9/12/16, 3:25:30 PM Central European Summer Time
Current location: <+51.50998000,-0.13370000> +/- 5.00m (speed -1.00 mps / course -1.00) @ 9/12/16, 3:25:31 PM Central European Summer Time
Current location: <+51.50998000,-0.13370000> +/- 5.00m (speed -1.00 mps / course -1.00) @ 9/12/16, 3:25:32 PM Central European Summer Time

You can download the source code of the IOS10RequestingPermissionTutorial at the ioscreator repository on Github.