Asking for Permission

To register a geofence in iOS, we first need a location and a radius. For the location, we use the current location of the device when the user adds the geofence by tapping a button. This is much easier for us than adding a map or some other user interface element for the input of the location. As we did in Chapter 1, Drawing on Maps, we add a class to manage the location updates.

Use the shortcut N to add a new file to the project, select the Cocoa Touch Class template, and click Next. Type in the class name LocationProvider, make it a subclass of NSObject, and click Next. In the storage location window, click Create.

We are going to access the location of the device through an instance of the class CLLocationManager defined in the framework CoreLocation. In addition we want to use the logging library we built in Appendix 1, Debugging on the Go, to make the log accessible when we’re testing our app outside. In the new file, import CoreLocation and the LogStore logging library:

 import​ ​UIKit
 import​ ​CoreLocation
 import​ ​LogStore

As the location provider will act as the delegate for a location manager, it needs to conform to the CLLocationManagerDelegate protocol. Add the CLLocationManagerDelegate protocol to the class declaration of LocationProvider so that it looks like the following code:

 class​ ​LocationProvider​: ​NSObject​,
 CLLocationManagerDelegate​ {
 }

Then add the highlighted code to LocationProvider:

 class​ ​LocationProvider​: ​NSObject​,
 CLLocationManagerDelegate​ {
»let​ locationManager: ​CLLocationManager
 
»override​ ​init​() {
»
» locationManager = ​CLLocationManager​()
»
»super​.​init​()
»
» locationManager.delegate = ​self
» locationManager.​requestAlwaysAuthorization​()
» }
 }

In the initializer we first assign a new instance of CLLocationManager to the locationManager property. Because LocationProvider is a subclass of NSObject, we need to call super.init before we can access self. That’s why we set the delegate of locationManager after the call to super.init. The last step is to ask for permission to always request the location of the device. We need Always authorization because when the geofence triggers, code executes while the app isn’t running. This means we need authorization from the user to access the location of the device when the app is not active.

When we call locationManager.requestAlwaysAuthorization, iOS presents a system alert to the user to authorize access to the device location. The user can confirm, allow once, or dismiss that request. To be notified of the user’s choice, we implement the delegate method locationManager(_:didChangeAuthorization:) in LocationProvider:

 func​ ​locationManager​(_ manager: ​CLLocationManager​,
  didChangeAuthorization status:
 CLAuthorizationStatus​) {
 
 switch​ status {
 case​ .authorizedAlways:
 printLog​(​"success"​)
 case​ .notDetermined:
 printLog​(​"notDetermined"​)
 default​:
 // FIXME: add code
 break
  }
 }

Before the user has the chance to react to the authorization alert, this method is called with the status notDetermined and we print the word notDetermined to the log. If the user authorizes the app to always use the location, we print the word success to the log. We’ll implement the default case at the end of this chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.36.192