iOS 9 Swift Programming Cookbook (2015)
Chapter 8. Maps and Location
In this chapter, we will have a look at some awesome updates to MapKit and CoreLocation frameworks.
8.1 Requesting the User’s Location a Single Time
Problem
You want an optimized and energy-efficient way of requesting the current location of the user only once.
Solution
Use the requestLocation() method of the CLLocationManager class. The new location will be sent to your location manager’s locationManager(_:didUpdateLocations:) delegate method. Errors will be reported on locationManager(_:didFailWithError:). You can make only one request to this method at any given time. A new request will cancel the previous one.
Discussion
Place a button on your interface inside IB and then hook it up to a method in your code called requestLocation(). Then go into your Info.plist file and set the value of the NSLocationWhenInUseUsageDescription key to a valid string that explains to the user why you want to get her location. You will also have to import the CoreLocation framework and make your view controller conform to CLLocationManagerDelegate.
Implement a variable in your view controller to represent the location manager:
lazy var locationManager: CLLocationManager = {
let m = CLLocationManager()
m.delegate = self
m.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
return m
}()
When your button is pressed, request access to the user’s location. This requests users location to be delivered to your app only when it is the foreground app. As soon as your app is sent to the background, iOS stops delivering location updates to you:
@IBAction func requestLocation() {
locationManager.requestWhenInUseAuthorization()
}
Then wait for the user to accept or reject the request. If everything is going smoothly, request the user’s location:
func locationManager(manager: CLLocationManager,
didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if case .AuthorizedWhenInUse = status{
manager.requestLocation()
} else {
//TODO: we didn't get access, handle this
}
}
Last but not least, wait for the location gathering mechanism to fail or succeed:
func locationManager(manager: CLLocationManager,
didUpdateLocations locations: [CLLocation]) {
//TODO: now you have access to the location. do your work
}
func locationManager(manager: CLLocationManager,
didFailWithError error: NSError) {
//TODO: handle the error
}
See Also