Book Image

Swift by Example

By : Giordano Scalzo
Book Image

Swift by Example

By: Giordano Scalzo

Overview of this book

Table of Contents (15 chapters)

Geolocalising the app


As a test, we have used dummy coordinates, but we do have a powerful GPS on board, and it's time to use it.

Using CoreLocation

To use the location service, we need to instruct iOS that our app is using it.

To do this, we must add the NSLocationAlwaysUsageDescription key with a string, for example, "This application requires location services to get the weather of your current location." in Info.plist.

Then, we add a new property to PrettyWeatherViewController:

    private var locationDatastore: LocationDatastore?

Next, we change the viewWillAppear function:

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        locationDatastore = LocationDatastore() { [weak self] location in
            FlickrDatastore().retrieveImageAtLat(location.lat, lon: location.lon){ image in
                self?.render(image)
                return
            }
        }
    }

Our simple wrapper around LocationManager basically calls the provided closure...