Check if location services are enabled

前端 未结 9 1688
情话喂你
情话喂你 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条回答
  •  不思量自难忘°
    2020-12-02 09:21

    Add the CLLocationManagerDelegate to your class inheritance and then you can make this check:

    Swift 1.x - 2.x version:

    if CLLocationManager.locationServicesEnabled() {
        switch CLLocationManager.authorizationStatus() {
        case .NotDetermined, .Restricted, .Denied:
            print("No access")
        case .AuthorizedAlways, .AuthorizedWhenInUse:
            print("Access")
        }
    } else {
        print("Location services are not enabled")
    }
    

    Swift 4.x version:

    if CLLocationManager.locationServicesEnabled() {
         switch CLLocationManager.authorizationStatus() {
            case .notDetermined, .restricted, .denied:
                print("No access")
            case .authorizedAlways, .authorizedWhenInUse:
                print("Access")
            }
        } else {
            print("Location services are not enabled")
    }
    

    Swift 5.1 version

    if CLLocationManager.locationServicesEnabled() {
        switch CLLocationManager.authorizationStatus() {
            case .notDetermined, .restricted, .denied:
                print("No access")
            case .authorizedAlways, .authorizedWhenInUse:
                print("Access")
            @unknown default:
            break
        }
        } else {
            print("Location services are not enabled")
    }
    

提交回复
热议问题