0

I have an application that finds a person coords and uses them for some code, of course if I do not have location enabled I cannot use that, but I want to check if it is enabled later on the site. Example:

User logs in, goes to my tab, has no location so it doesn't show nearest shop, but once he enables location there, a listener tells my code that its on and does stuff accordingly?

I was thinking of using a timer to check every second... but that seems kinda stupid, is there any alternatives? What would be good to implement?

Alistra
  • 5,177
  • 2
  • 30
  • 42

1 Answers1

0

In your screen where you have required location, just check whether location service is enabled or not.

override func viewDidLoad() 
{
    super.viewDidLoad()
    self.checkLocationServiceEnabled()
}

func checkLocationServiceEnabled()
{
    let locationAuthorizationStatus = CLLocationManager.authorizationStatus()

    switch locationAuthorizationStatus {

    case .AuthorizedAlways ,.AuthorizedWhenInUse ,.NotDetermined ,.Restricted:
        break
    case .Denied:
        self.alertTurnOnLocationFromDevicePrivacySetting()
    }
}

func alertTurnOnLocationFromDevicePrivacySetting()
{
    let delayTime = dispatch_time(DISPATCH_TIME_NOW,
                                  Int64(1 * Double(NSEC_PER_SEC)))
    dispatch_after(delayTime, dispatch_get_main_queue()) {
            let alertController = UIAlertController(
                title: ALERT_TURN_ON_LOCATION_FROM_DEVICE_PRIVACY,
                message: "",
                preferredStyle: .Alert)

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

            self.navigationController?.topViewController!.presentViewController(alertController, animated: true, completion: nil)
    }
}
Hardik Shekhat
  • 1,680
  • 12
  • 21