1

I have requestAlwaysAuthorization and I need to track users every time if user doesn't accept to requestAlwaysAuthorization I want to do exit in app ?

How can I do it ?

My codes under below.

import CoreLocation

 public var locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()


        locationManager.delegate = self
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()

}



    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        let altitudeG = locations.last?.altitude
        let longitudeG = locations.last?.coordinate.longitude
        let latitudeG = locations.last?.coordinate.latitude

print("\(altitudeG) \(longitudeG) \(latitudeG)")

    }
shallowThought
  • 19,212
  • 9
  • 65
  • 112
SwiftDeveloper
  • 7,244
  • 14
  • 56
  • 85

2 Answers2

1

In error case this delegatemethod is called:

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
    print(error)
    // handle not authorized error here. You might want to quit on specific errors (unauthorized) only. Check the error.

    UIControl().sendAction(#selector(URLSessionTask.suspend), to: UIApplication.shared, for: nil) 
}

You can also check the current permissions state before letting CLLocationManager fail:

if CLLocationManager.locationServicesEnabled() {
    switch(CLLocationManager.authorizationStatus()) {
        case .notDetermined, .restricted, .denied:
            print("No access")
        case .authorizedAlways, .authorizedWhenInUse:
            print("Access")
        }
    } else {
        print("Location services are not enabled")
}

taken from this answer.


Opinion based: I consider quitting the app instead of giving the user a understandable feedback very bad UX.

Community
  • 1
  • 1
shallowThought
  • 19,212
  • 9
  • 65
  • 112
  • 1
    Sidenote: If your app just "quits" instead of showing an error message, it will make Apple reject if from the App Store. Even more, blocking users from accessing your app if they don't give you some permissions is a rejection cause too. – Alejandro Iván Apr 04 '17 at 15:41
1

Above answer is also good, I just tried to make it bit easy way with methods. Also, if you are working with hardware devices like beacons then you must access the location AuthorizedAlways.

Check if location services are enabled

 public func isLocationEnabled()-> Bool {

    if CLLocationManager.locationServicesEnabled() {
        switch(CLLocationManager.authorizationStatus()) {
        case .NotDetermined, .Restricted, .Denied , .AuthorizedWhenInUse :
            showLocationServiceNotEnabledAlert()
            return false
        case .AuthorizedAlways: // As of now we check for only "Always", not for "When In Use" this should be fixed according to the requirements
            return true
        }
    }

    return false
}

Alert For User to on the service And redirect to Settings

func showLocationServiceNotEnabledAlert() {
    let title = "Your Title"
    let message = "Your Message"
    let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)

    let settingsAction = UIAlertAction(title: "Settings".localized, style: .Default) { (alertAction) in
        if let appSettings = NSURL(string: UIApplicationOpenSettingsURLString) {
            UIApplication.sharedApplication().openURL(appSettings)
        }
    }
    alertController.addAction(settingsAction)

    let cancelAction = UIAlertAction(title: "Cancel".localized, style: .Cancel, handler: nil)
    alertController.addAction(cancelAction)

    UIApplication.sharedApplication().delegate?.window!?.currentViewController?.presentViewController(alertController, animated: true, completion: nil)
}