Check if location services are enabled

前端 未结 9 1696
情话喂你
情话喂你 2020-12-02 08:34

I\'ve been doing some research about CoreLocation. Recently, I encountered a problem that has been covered elsewhere, but in Objective C, and for iOS 8.

I feel kinda

9条回答
  •  Happy的楠姐
    2020-12-02 09:21

    Here is the format Apple recommends.

      switch CLLocationManager.authorizationStatus() {
          case .notDetermined:
             // Request when-in-use authorization initially
             break
          case .restricted, .denied:
             // Disable location features
             break
          case .authorizedWhenInUse, .authorizedAlways:
             // Enable location features
             break
          }
    

    Here is a complete example.

    This includes an AlertView with a button to take the user to the Settings screen if previously denied access.

    import CoreLocation
    let locationManager = CLLocationManager()
    
    class SettingsTableViewController:CLLocationManagerDelegate{
    
        func checkUsersLocationServicesAuthorization(){
            /// Check if user has authorized Total Plus to use Location Services
            if CLLocationManager.locationServicesEnabled() {
                switch CLLocationManager.authorizationStatus() {
                case .notDetermined:
                    // Request when-in-use authorization initially
                    // This is the first and the ONLY time you will be able to ask the user for permission
                    self.locationManager.delegate = self
                    locationManager.requestWhenInUseAuthorization()
                    break
    
                case .restricted, .denied:
                    // Disable location features
                    switchAutoTaxDetection.isOn = false
                    let alert = UIAlertController(title: "Allow Location Access", message: "MyApp needs access to your location. Turn on Location Services in your device settings.", preferredStyle: UIAlertController.Style.alert)
    
                    // Button to Open Settings
                    alert.addAction(UIAlertAction(title: "Settings", style: UIAlertAction.Style.default, handler: { action in
                        guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else {
                            return
                        }
                        if UIApplication.shared.canOpenURL(settingsUrl) {
                            UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
                                print("Settings opened: \(success)") 
                            })
                        }
                    }))
                    alert.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: nil))
                    self.present(alert, animated: true, completion: nil)
    
                    break
    
                case .authorizedWhenInUse, .authorizedAlways:
                    // Enable features that require location services here.
                    print("Full Access")
                    break
                }
            }
        }
    }
    

提交回复
热议问题