Why is the default value set to CLLocationManager.locationServicesEnabled ()?

I noticed the following with a simple, brand new project in Xcode.

If I import CoreLocation in the ViewController.swift file, and then add ... to the viewDidLoad method ...

print (CLLocationManager.locationServicesEnabled ())

... when the application starts in the Xcode simulator it displays true. I would have thought that location services would be disabled by default, but as you can see for yourself, this is true. If I wanted, I could add some more code to collect information about the user's location and all this without the need to request permission.

Can someone explain why this is?

+4
source share
2 answers

As far as I know, CLLocationManager.locationServicesEnabled () will return whether location services on the device are allowed for more than just one application. Therefore, even if location services are disabled for this application, if they are enabled for the device, I think it will return true anyway, according to the documentation: https://developer.apple.com/library/ios/documentation/CoreLocation/Reference / CLLocationManager_Class / # // apple_ref / occ / clm / CLLocationManager / locationServicesEnabled

In my application, I configured it like this:

    //check if location services are enabled at all
    if CLLocationManager.locationServicesEnabled() {

        switch(CLLocationManager.authorizationStatus()) {
        //check if services disallowed for this app particularly
        case .Restricted, .Denied:
            print("No access")
            var accessAlert = UIAlertController(title: "Location Services Disabled", message: "You need to enable location services in settings.", preferredStyle: UIAlertControllerStyle.Alert)

            accessAlert.addAction(UIAlertAction(title: "Okay!", style: .Default, handler: { (action: UIAlertAction!) in UIApplication.sharedApplication().openURL(NSURL(string:UIApplicationOpenSettingsURLString)!)
            }))

            presentViewController(accessAlert, animated: true, completion: nil)

        //check if services are allowed for this app
        case .AuthorizedAlways, .AuthorizedWhenInUse:
            print("Access! We're good to go!")
        //check if we need to ask for access
        case .NotDetermined:
            print("asking for access...")
            manager.requestAlwaysAuthorization()
        }
    //location services are disabled on the device entirely!
    } else {
        print("Location services are not enabled")

    }

Good luck

+11
source

Swift 3.1 function for returning status and error message

func isLocationEnabled() -> (status: Bool, message: String) {
    if CLLocationManager.locationServicesEnabled() {
        switch(CLLocationManager.authorizationStatus()) {
        case .notDetermined, .restricted, .denied:
            return (false,"No access")
        case .authorizedAlways, .authorizedWhenInUse:
            return(true,"Access")
        }
    } else {
        return(false,"Turn On Location Services to Allow App to Determine Your Location")
    }
}
0
source

Source: https://habr.com/ru/post/1625683/


All Articles