问题描述
我一直在研究CoreLocation。最近,我遇到了其他地方已经涉及的问题,但是在Objective C和iOS 8中。
我觉得有点傻问这个,但你怎么能检查一下在iOS 9上使用swift启用位置服务?
在iOS 7(可能是8?)上你可以使用 locationServicesEnabled()
,但在编译iOS 9时似乎没有用。
那我该如何实现呢?
谢谢!
添加 CLLocationManagerDelegate
你的班级继承,然后你可以进行这项检查:
Swift 1.x - 2.x版本:
'pre>
如果CLLocationManager.locationServicesEnabled(){
开关CLLocationManager.authorizationStatus(){
情况下.NotDetermined,.Restricted,.Denied:
打印(无法访问)
case .AuthorizedAlways,.AuthorizedWhenInUse:
print(Access)
}
} else {
print(位置服务未启用)
}
Swift 3.0版本:
如果CLLocationManager.locationServicesEnabled(){
切换CLLocationManager。 authorizationStatus(){
情况下.notDetermined,.restricted,.denied:
打印( 无法访问)
情况下.authorizedAlways,.authorizedWhenInUse:
打印( 访问)
}
}其他{
打印(位置服务未启用)
}
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 silly asking this, but how can you check if location services are enabled using swift, on iOS 9?
On iOS 7 (and maybe 8?) you could use locationServicesEnabled()
, but that doesn't appear to be working when compiling for iOS 9.
So how would I accomplish this?
Thanks!
Add the CLLocationManagerDelegate
to your class inheritance and then you can make this check:
Swift 1.x - 2.x version:
if CLLocationManager.locationServicesEnabled() {
switch CLLocationManager.authorizationStatus() {
case .NotDetermined, .Restricted, .Denied:
print("No access")
case .AuthorizedAlways, .AuthorizedWhenInUse:
print("Access")
}
} else {
print("Location services are not enabled")
}
Swift 3.0 version:
if CLLocationManager.locationServicesEnabled() {
switch CLLocationManager.authorizationStatus() {
case .notDetermined, .restricted, .denied:
print("No access")
case .authorizedAlways, .authorizedWhenInUse:
print("Access")
}
} else {
print("Location services are not enabled")
}
这篇关于检查是否已启用位置服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!