Check if location services are enabled

前端 未结 9 1660
情话喂你
情话喂你 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:26

    To ask for permission for location services you use:

    yourSharedLocationManager.requestWhenInUseAuthorization()
    

    If the status is currently undetermined an alert will show prompting the user to allow access. If access is denied your app will be notified in the CLLocationManagerDelegate, likewise if permission is denied at any point you will be updated here.

    There are two separate statuses you need to check to determine the current permissions.

    • If the user has the general location services enabled or not

    CLLocationManager.locationServicesEnabled()

    • If the user has granted the correct permission for your app..

    CLLocationManager.authorizationStatus() == .authorizedWhenInUse

    You could add an extension is a handy option:

    extension CLLocationManager {
    static func authorizedToRequestLocation() -> Bool {
        return CLLocationManager.locationServicesEnabled() &&
            (CLLocationManager.authorizationStatus() == .authorizedAlways || CLLocationManager.authorizationStatus() == .authorizedWhenInUse)
    }
    

    }

    Here it is being accessed when the user has first requested directions:

     private func requestUserLocation() {
        //when status is not determined this method runs to request location access
        locationManager.requestWhenInUseAuthorization()
    
        if CLLocationManager.authorizedToRequestLocation() {
    
            //have accuracy set to best for navigation - accuracy is not guaranteed it 'does it's best'
            locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
    
            //find out current location, using this one time request location will start the location services and then stop once have the location within the desired accuracy -
            locationManager.requestLocation()
        } else {
            //show alert for no location permission
            showAlertNoLocation(locationError: .invalidPermissions)
        }
    }
    

    Here is the delegate:

     func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    
        if !CLLocationManager.authorizedToRequestLocation() {
            showAlertNoLocation(locationError: .invalidPermissions)
        }
    }
    
    0 讨论(0)
  • 2020-12-02 09:27

    In Swift 3.0

    if (CLLocationManager.locationServicesEnabled())
                {
                    locationManager.delegate = self
                    locationManager.desiredAccuracy = kCLLocationAccuracyBest
                    if ((UIDevice.current.systemVersion as NSString).floatValue >= 8)
                    {
                        locationManager.requestWhenInUseAuthorization()
                    }
    
                    locationManager.startUpdatingLocation()
                }
                else
                {
                    #if debug
                        println("Location services are not enabled");
                    #endif
                }
    
    0 讨论(0)
  • 2020-12-02 09:29

    SWIFT (As of July 24, 2018)

    if CLLocationManager.locationServicesEnabled() {
    
    }
    

    this will tell you if the user has already selected a setting for the app's location permission request

    0 讨论(0)
提交回复
热议问题