检查位置服务是否启用
我一直在做一些关于CoreLocation的研究。 最近,我遇到了其他地方已经涉及到的问题,但在Objective C和iOS 8中。
我觉得有点傻这么问,但是如何在iOS 9上检查位置服务是否使用swift启用?
在iOS 7上(也许是8?),你可以使用locationServicesEnabled()
,但是在编译iOS 9时,这似乎不起作用。
那么我怎么做到这一点?
谢谢!
添加CLLocationManagerDelegate
到你的类inheritance,然后你可以做这个检查:
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") }
Swift 3.0版本:
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 (截至2017年7月4日)
if CLLocationManager.locationServicesEnabled() { }
这会告诉你,如果用户已经放下了位置的设置
当您调用-startLocation时,如果位置服务被用户拒绝,则位置pipe理器委托将接收到对 – locationManager:didFailWithError
:的调用,并带有kCLErrorDenied
错误代码。 这适用于所有版本的iOS。
在目标-c
你应该跟踪用户已经否认或不确定,然后要求许可或发送用户设置应用程序。
-(void)askEnableLocationService { BOOL showAlertSetting = false; BOOL showInitLocation = false; if ([CLLocationManager locationServicesEnabled]) { switch ([CLLocationManager authorizationStatus]) { case kCLAuthorizationStatusDenied: showAlertSetting = true; NSLog(@"HH: kCLAuthorizationStatusDenied"); break; case kCLAuthorizationStatusRestricted: showAlertSetting = true; NSLog(@"HH: kCLAuthorizationStatusRestricted"); break; case kCLAuthorizationStatusAuthorizedAlways: showInitLocation = true; NSLog(@"HH: kCLAuthorizationStatusAuthorizedAlways"); break; case kCLAuthorizationStatusAuthorizedWhenInUse: showInitLocation = true; NSLog(@"HH: kCLAuthorizationStatusAuthorizedWhenInUse"); break; case kCLAuthorizationStatusNotDetermined: showInitLocation = true; NSLog(@"HH: kCLAuthorizationStatusNotDetermined"); break; default: break; } } else { showAlertSetting = true; NSLog(@"HH: locationServicesDisabled"); } if (showAlertSetting) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"Please enable location service for this app in ALLOW LOCATION ACCESS: Always, Go to Setting?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Open Setting", nil]; alertView.tag = 199; [alertView show]; } if (showInitLocation) { [self initLocationManager]; } }
如果已被用户拒绝,则实施alertView委托,然后发送用户启用位置服务。
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
初始位置pipe理器
-(void)initLocationManager{ self.locationManager = [[CLLocationManager alloc] init]; if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) { [self.locationManager requestAlwaysAuthorization]; } }
请注意kCLAuthorizationStatusAuthorizedAlways和kCLAuthorizationStatusAuthorizedWhenInUse是不同的。
对于swift3.0及以上版本,如果经常检查位置服务的可用性,请创build一个如下所示的类,
import CoreLocation open class Reachability { class func isLocationServiceEnabled() -> Bool { if CLLocationManager.locationServicesEnabled() { switch(CLLocationManager.authorizationStatus()) { case .notDetermined, .restricted, .denied: return false case .authorizedAlways, .authorizedWhenInUse: return true default: print("Something wrong with Location services") return false } } else { print("Location services are not enabled") return false } } }
然后在VC中像这样使用它
if Reachability.isLocationServiceEnabled() == true { // Do what you want to do. } else { //You could show an alert like this. let alertController = UIAlertController(title: "Location Services Disabled", message: "Please enable location services for this app.", preferredStyle: .alert) let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil) alertController.addAction(OKAction) OperationQueue.main.addOperation { self.present(alertController, animated: true, completion:nil) } }
这只是Swift 4中的一个2行函数:
import CoreLocation static func isLocationPermissionGranted() -> Bool { guard CLLocationManager.locationServicesEnabled() else { return false } return [.authorizedAlways, .authorizedWhenInUse].contains(CLLocationManager.authorizationStatus()) }