如何检查地理位置是否开启?

时间:2015-11-29 19:54:48

标签: ios swift core-location

我有一个button,可以获取我当前的位置,但是当我点击它并关闭地理位置时,应用程序应用程序崩溃了。

我想写一个案例,如果用户关闭了他的地理数据,我建议用户通过特殊警报窗口打开它。

是否有针对此类问题的corelocation方法?

3 个答案:

答案 0 :(得分:2)

在初始化CLLocationManager之后不久调用

locationManager:didChangeAuthorizationStatus

所以添加此函数以获取位置状态:

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    switch status {
    case CLAuthorizationStatus.Restricted:
        print("Restricted")
    case CLAuthorizationStatus.Denied:
        print("Denied")
    default:
        break
    }
}

Swift 4语法:

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    switch status {
    case .restricted:
        print("restricted")
    case .denied:
        print("denied")
    case .notDetermined:
        print("notDetermined")
    case .authorizedAlways:
        print("authorizedAlways")
    case .authorizedWhenInUse:
        print("authorizedWhenInUse")
    }
}

Apples reference to CLAuthorizationStatus.

答案 1 :(得分:0)

当您致电 - startLocation时,如果用户拒绝了位置服务,则位置管理员代表将收到- locationManager:didFailWithError:的{​​{1}}错误代码调用。

答案 2 :(得分:0)

在尝试获取位置之前处理位置状态:

// create enum to check
enum Usage {
        case WhenInUse
        case Always
    }

func checkLocationAuthorization(usage: Usage) {
    let enabled = CLLocationManager.locationServicesEnabled()
            let actual = CLLocationManager.authorizationStatus()

            var error: NSError?

            // There are several factors to consider when evaluating this condition
            switch (enabled, usage, actual) {
                case (true, _, .AuthorizedAlways):
                    // The service is enabled, and we have "Always" permission -> condition satisfied.
                    break

                case (true, .WhenInUse, .AuthorizedWhenInUse):
                    /*
                        The service is enabled, and we have and need "WhenInUse"
                        permission -> condition satisfied.
                    */
                    break

                default:
                    /*
                        Anything else is an error. Maybe location services are disabled,
                        or maybe we need "Always" permission but only have "WhenInUse",
                        or maybe access has been restricted or denied,
                        or maybe access hasn't been request yet.

                        The last case would happen if this condition were wrapped in a `SilentCondition`.
                    */
                    error = NSError(code: .ConditionFailed, userInfo: [
                        OperationConditionKey: self.dynamicType.name,
                        self.dynamicType.locationServicesEnabledKey: enabled,
                        self.dynamicType.authorizationStatusKey: Int(actual.rawValue)
                    ])
            }

            if let error = error {
                // Handle not enabled condition here
            }
            else {
                // Location is enabled and continue the user actions
            }

}