Request Permission in Core Location iOS Tutorial
In 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 10 and built for iOS 12.
Open Xcode and create a new Single View App.
For product name, use IOSRequestPermissionTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and choose Next.
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.
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 getLocation(_:) method.
@IBAction func getLocation(_ sender: Any) { // 1 let status = CLLocationManager.authorizationStatus() switch status { // 1 case .notDetermined: locationManager.requestWhenInUseAuthorization() return // 2 case .denied, .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 case .authorizedAlways, .authorizedWhenInUse: break } // 4 locationManager.delegate = self locationManager.startUpdatingLocation() }
The authorizationStatus returns the current authorisation status.
The when in use authorisation get location updates while the app is in the foreground
When location services is disabled, the user will be shown an alert
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]) { if let currentLocation = locations.last { print("Current location: \(currentLocation)") } } // 2 func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { print(error) }
The current location is printed to the console.
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 IOSRequestPermissionTutorial at the ioscreator repository on Github.