Toggle in-app GPS UISwitch, when the user toggles the app's location services

194 views Asked by At

The idea is to toggle the in-app GPS switch when the user toggles the app's location services from the Settings screen.

This is my Model

func locationManager(manager:CLLocationManager!,didChangeAuthorizationStatus status: CLAuthorizationStatus){
switch status{
case .NotDetermined:
    println("Not Determined!")
    break
case .AuthorizedAlways:                            // It is being authorized always. If this is a security breach or causes
    println("Authorized Always!")
    NSNotificationCenter.defaultCenter().postNotificationName("GpsOn", object: nil)
    self.locationManager.startUpdatingLocation()   // Starts Updating Location in terms of latitude, longitude
    break
case .Denied:
    println("Denied!")
    break
default:
    println("Unhandled authorization status error")
    break
}

}

And the ViewController

override func viewDidLoad() {
        super.viewDidLoad()
        self.initMenuWithTitle("Settings")
        settingsArray = ["Filters","Notification","GPS"]
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("toggleGps:"), name: "gpsOn", object: self)

    }

func toggleGps(object:AnyObject){
        println("gps on")
    }
1

There are 1 answers

3
Mark On

CLLocationManager might be what you're looking for:

func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
  // stuff
}