Ios 为什么是';CLLocationManager.locationServicesEnabled();默认情况下为true?
我在Xcode中的一个简单、全新的项目中注意到了以下内容 如果在ViewController.swift文件中导入CoreLocation,然后在viewDidLoad方法中添加 打印(CLLocationManager.locationServicesEnabled()) ,当应用程序在模拟器中运行时,Xcode打印出true。我原以为默认情况下位置服务会被禁用,但正如你自己所看到的,情况恰恰相反。如果我愿意,我可以添加更多的代码来收集有关用户的位置信息,所有这些都不需要请求许可Ios 为什么是';CLLocationManager.locationServicesEnabled();默认情况下为true?,ios,swift,core-location,cllocationmanager,Ios,Swift,Core Location,Cllocationmanager,我在Xcode中的一个简单、全新的项目中注意到了以下内容 如果在ViewController.swift文件中导入CoreLocation,然后在viewDidLoad方法中添加 打印(CLLocationManager.locationServicesEnabled()) ,当应用程序在模拟器中运行时,Xcode打印出true。我原以为默认情况下位置服务会被禁用,但正如你自己所看到的,情况恰恰相反。如果我愿意,我可以添加更多的代码来收集有关用户的位置信息,所有这些都不需要请求许可 有人能解释一
有人能解释一下原因吗?据我所知,CLLocationManager.locationServicesEnabled()将返回设备上是否启用了位置服务,而不仅仅是该应用程序。因此,即使该应用程序禁用了位置服务,但如果该设备启用了位置服务,我认为根据文档,这仍然会返回true: 在我的应用程序中,我将其设置为:
//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返回状态和错误消息的功能
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")
}
}
完美的正是我需要的知识。太棒了!如果我帮忙,你可以把它标记为答案:)