O'Reilly logo

iOS 9 Swift Programming Cookbook by Vandad Nahavandipoor

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 8. Maps and Location

In this chapter, we will have a look at some awesome updates to the 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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required