3

The problem I am facing is that when I press a UIButton - location services are required to initiate the action. However if the user was to deny Location Services at the initial launch of the app - the app will crash.

I have tried finding a way to implement CLAuthorizationStatus .Denied but I can't seem to find a way to do so. The only code I can seem to implement is the didChangeAuthorizationStatus which only initiates the request at First Launch of the application.

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus)
{
    if status == .AuthorizedAlways || status == .AuthorizedWhenInUse
    {
        manager.startUpdatingLocation()
    }
    else
    {
        manager.requestWhenInUseAuthorization()
    }
}

If I press the UIButton to send the API Request the app will crash if location services have been denied.

My question is how can I implement a method, within the button's IBAction, that will direct the user to go to their settings and enable location services. :)

lifewithelliott
  • 1,012
  • 2
  • 16
  • 35
  • What problem are you trying to resolve exactly? – nhgrif Apr 17 '16 at 22:58
  • If permission to use location services denied and button is pressed -> present alert to state that user location is required to use application and redirect the user to the settings to change it. – lifewithelliott Apr 17 '16 at 23:02
  • And what happened when you tried `else if status == .Denied`? – nhgrif Apr 17 '16 at 23:02
  • When I tried if status == .Denied nothing had actually changed and it never presented the controller I tried placing in the brackets. this method will appear when the app is initially installed and runs on its own. I am not sure how or even if i can create an instance of CLAuthorizationStatus in the button's IBAction method. – lifewithelliott Apr 17 '16 at 23:06
  • Ive tried within the IBAction - let status : CLAuthorization status and implement a switch statement to attempt switch status { case .Denied ....etc... but didn't find luck in that approach – lifewithelliott Apr 17 '16 at 23:16

1 Answers1

5

CLLocationManager has a static function authorizationStatus() that you can use to get the current authorization status without even initializing a CLLocationManager object.

So in the function that you call when the user presses the button you can check the authorization status and act accordingly:

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

    lazy var locationManager = CLLocationManager()

    ...

    func didPressButton(sender: UIButton) {
        switch CLLocationManager.authorizationStatus() {
        case .AuthorizedAlways, .AuthorizedWhenInUse:
            locationManager.delegate = self
            locationManager.startUpdatingLocation()
        case .NotDetermined:
            locationManager.delegate = self
            locationManager.requestWhenInUseAuthorization()
        case .Denied:
            print("Show Alert with link to settings")
        case .Restricted:
            // Nothing you can do, app cannot use location services
            break
        }
    }

    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        if status == .AuthorizedWhenInUse {
            manager.startUpdatingLocation()
        }
    }
}
joern
  • 27,354
  • 7
  • 90
  • 105