Get User Location Using CLLocationManager

suggest change
  1. Include CoreLocation in your project. In older Xcode projects this is Build Phases, Link Binary With Libraries: click the (+) button, choose CoreLocation.framework, and click Add. With modules, import CoreLocation is enough.

  2. Ask for permission in Info.plist. Open it as source code and add a key under the top-level <dict>. For location while the app is in use:

<key>NSLocationWhenInUseUsageDescription</key>
<string>message to display when asking for permission</string>

“Always” authorization needs NSLocationAlwaysAndWhenInUseUsageDescription (and, on older systems, NSLocationAlwaysUsageDescription) instead of, or in addition to, the When-In-Use key.

  1. Import CoreLocation in the view controller that will use it.
import CoreLocation
  1. Conform to CLLocationManagerDelegate. The delegate method below is Swift 2. requestLocation also requires locationManager(_:didFailWithError:); without it, Core Location raises an exception.
class ViewController: UIViewController, CLLocationManagerDelegate {}

After these steps, create a CLLocationManager as an instance variable and use it in the view controller.

var manager: CLLocationManager!

This is not let, because the code below assigns the manager and then sets its delegate, distance filter, and accuracy.

//initialize the manager
manager = CLLocationManager()

//specify delegate
manager.delegate = self

//set the minimum distance the phone needs to move before an update event is triggered (for example: 100 meters)
manager.distanceFilter = 100

//set Accuracy to any of the following depending on your use case

//let kCLLocationAccuracyBestForNavigation: CLLocationAccuracy
//let kCLLocationAccuracyBest: CLLocationAccuracy
//let kCLLocationAccuracyNearestTenMeters: CLLocationAccuracy
//let kCLLocationAccuracyHundredMeters: CLLocationAccuracy
//let kCLLocationAccuracyKilometer: CLLocationAccuracy
//let kCLLocationAccuracyThreeKilometers: CLLocationAccuracy

manager.desiredAccuracy = kCLLocationAccuracyBest

//ask the user for permission; the prompt is asynchronous, and no fixes arrive until the user allows it
manager.requestWhenInUseAuthorization()

//Start collecting location information
if #available(iOS 9.0, *) {
    manager.requestLocation()
} else {
    manager.startUpdatingLocation()
}

To receive location updates, implement the delegate method below. It is called when distanceFilter is reached, and requestLocation calls it once for a single fix.

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {}

The locations parameter is an array of CLLocation objects. From these objects you can read coordinate, altitude, floor, horizontalAccuracy, verticalAccuracy, timestamp, description, course, and speed, and call distance(from:) to measure the distance between two locations.

While requesting permission, there are two authorization types.

“When In Use” authorization only gives the app permission to receive your location when the app is in use or in the foreground.

“Always” authorization gives the app background permission, which can reduce battery life when the app is not on screen.

Adjust the plist keys to match the authorization you request.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents