为什么 'CLLocationManager.locationServicesEnabled()' 默认为真?

Why is 'CLLocationManager.locationServicesEnabled()' true by default?

我在 Xcode.

中注意到一个简单的全新项目的以下内容

如果,在ViewController.swift文件中导入CoreLocation,然后在viewDidLoad方法中添加...

打印(CLLocationManager.locationServicesEnabled())

...,当应用程序在模拟器中运行时 Xcode 打印出 true。我原以为默认情况下会禁用定位服务,但正如您自己所见,事实恰恰相反。如果我愿意,我可以添加更多代码来收集有关用户的位置信息,而所有这些都无需请求许可。

谁能解释这是为什么?

据我所知,CLLocationManager.locationServicesEnabled() 将 return 定位服务是否在 设备上启用, 不仅针对那个应用程序。因此,即使为该应用程序禁用了位置服务,如果为设备启用了它们,我认为仍然 return 正确,根据文档:https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/#//apple_ref/occ/clm/CLLocationManager/locationServicesEnabled

在我的应用程序中,我是这样设置的:

    //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")

    }

祝你好运!

Swift 3.1函数到return状态和错误信息

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")
    }
}