locationServicesEnabled always return YES

ⅰ亾dé卋堺 提交于 2019-12-02 20:59:20

When you implement the delegate for location manager, you should be implementing didFailWithError. In there you will get the appropriate error if the user did not allow access to location

Apple Documentation States: If the user denies your application’s use of the location service, this method reports a kCLErrorDenied error. Upon receiving such an error, you should stop the location service.

Remember that [locationManager locationServicesEnabled] is deprecated since iOS 4.0. Use the Class Method [CLLocationManager locationServicesEnabled] instead.

The App Specific Button can be retrieved by

[CLLocationManager authorizationStatus]

When you use

[CLLocationManager locationServicesEnabled]

then you inspect if locationServices are enabled in whole system. So when you go to Settings -> Location Services and you see that first switch. That method returns state of that state and is not in relation with your app.

If you need to know if your app has access to location services use @Pascalius answer.

Swift 3.1 function returns -> status:Bool and message:String

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")
    }
}
if(![CLLocationManager locationServicesEnabled] || ([CLLocationManager authorizationStatus]!=kCLAuthorizationStatusAuthorizedWhenInUse && [CLLocationManager authorizationStatus]!=kCLAuthorizationStatusAuthorizedAlways))
{
        ; // app doesn't have access to localization to whatever you want
}

[CLLocationManager locationServicesEnabled] will return NO when the user setting button is switched to OFF, only then I have achieved a NO.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!