7

I've created a function which is suppose to handle the permission with location, so that the app will close if it does not have permission to location. However when you press open settings and press "back to app" in the status bar the determeinePermission method is not being executed again. I've tried to add it to viewDidLoad, viewDidAppear and viewWillAppear. what can I do?

func determinePermission() {
    switch CLLocationManager.authorizationStatus() {

    case .Authorized:
        if CLLocationManager.locationServicesEnabled() {
            manager.delegate = self
            manager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
            manager.startUpdatingLocation()
        }


    case .NotDetermined:
        manager.requestWhenInUseAuthorization()
    case .AuthorizedWhenInUse, .Restricted, .Denied:
        let alertController = UIAlertController(
            title: "Background Location Access Disabled",
            message: "In order to be notified about adorable kittens near you, please open this app's settings and set location access to 'Always'.",
            preferredStyle: .Alert)

        let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
            exit(0)
        }
        alertController.addAction(cancelAction)

        let openAction = UIAlertAction(title: "Open Settings", style: .Default) { (action) in
            if let url = NSURL(string:UIApplicationOpenSettingsURLString) {
                UIApplication.sharedApplication().openURL(url)
            }
        }
        alertController.addAction(openAction)

        self.presentViewController(alertController, animated: true, completion: nil)
    }
}
sagar.musale
  • 585
  • 1
  • 7
  • 19
Peter Pik
  • 11,023
  • 19
  • 84
  • 142

1 Answers1

6

Try adding it to UIApplicationDelegate.applicationDidBecomeActive.

ricardopereira
  • 11,118
  • 5
  • 63
  • 81
Gruntcakes
  • 37,738
  • 44
  • 184
  • 378
  • 1
    it is applicationDidBecomeActive not 'add' – Max Dec 07 '15 at 06:36
  • 1
    UIApplicationDidDidBecomeActive will also trigger after temporary interruptions like phone calls or SMS messages, so I would recommend UIApplicationDidEnterForeground instead. – Vadoff Jul 19 '16 at 05:19