0

I'm trying out the new CLLocationManager API which will contain "Allow Once". By default my UISwitch is off when I go to the native settings page and select "While Using the App" when I return to the app I want my switch to be enabled. The only time the switch should be off is when the user selects "Never" or "Don't allow" when requestWhenInUseAuthorization() is called.

import UIKit
import CoreLocation

class ViewController: UIViewController,CLLocationManagerDelegate {

    let locationManager = CLLocationManager()
    let status = CLLocationManager.authorizationStatus()

override func viewDidLoad() {
        locationManager.delegate = self
    }

@IBOutlet weak var toggleSwitch: UISwitch!

    @IBAction func locationToggle(_ sender: Any) {

        if status == .denied || status == .restricted && toggleSwitch.isOn {
            toggleSwitch.setOn(false, animated: false)
        }
        else if status == .authorizedWhenInUse || status == .authorizedAlways {
            toggleSwitch.setOn(true, animated: false)
        }
        else if status == .notDetermined {
            locationManager.requestWhenInUseAuthorization()
        }
    }
}

0 Answers0