我一直在做一些关于CoreLocation的研究。最近,我遇到了一个问题,该问题已在其他地方解决,但在Objective C和iOS 8中已涵盖。
我觉得这有点愚蠢,但是如何在iOS 9上检查是否使用swift启用了定位服务?
在iOS 7(甚至可能是8?)上,您可以使用locationServicesEnabled(),但是在为iOS 9编译时似乎无法正常工作。
locationServicesEnabled()
那我该怎么做呢?
谢谢!
将添加CLLocationManagerDelegate到您的类继承中,然后可以进行以下检查:
CLLocationManagerDelegate
Swift 1.x-2.x版本:
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") }
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") }
if CLLocationManager.locationServicesEnabled() { switch CLLocationManager.authorizationStatus() { case .notDetermined, .restricted, .denied: print("No access") case .authorizedAlways, .authorizedWhenInUse: print("Access") @unknown default: break } } else { print("Location services are not enabled") }