首先我们要向导入一个官方提供的库
#import <CoreLocation/CLLocationManager.h>
导入以后就可以写代码了,当然了为了方便起见,个人建议将下面的方法封装成一个工具类,这样的话在任何位置都可以调用我将该方法封装成了+方法(类方法),类名:NSXYCToolObject :NSObject
.h
/*
* 判断是否打开定位
*/
+ (BOOL)determineWhetherTheAPPOpensTheLocation;
.m
#pragma mark 判断是否打开定位
+(BOOL)determineWhetherTheAPPOpensTheLocation{
if ([CLLocationManagerlocationServicesEnabled] && ([CLLocationManagerauthorizationStatus] ==kCLAuthorizationStatusAuthorizedWhenInUse || [CLLocationManagerauthorizationStatus] ==kCLAuthorizationStatusNotDetermined || [CLLocationManagerauthorizationStatus] ==kCLAuthorizationStatusAuthorized)) {
returnYES;
}elseif ([CLLocationManagerauthorizationStatus] ==kCLAuthorizationStatusDenied) {
returnNO;
}else{
returnNO;
}
}
下面是调用返回值是YES,定位开启,NO,关闭:[NSXYCToolObjectdetermineWhetherTheAPPOpensTheLocation]
如果没开启,我们会弹框提示让他打开定位,进行下面的操作
if (![NSXYCToolObjectdetermineWhetherTheAPPOpensTheLocation]) {
UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"提示"message:@"请到设置->隐私->定位服务中开启【学易车】定位服务,以便于距离筛选能够准确获得你的位置信息" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"设置",nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{//点击弹窗按钮后
if (buttonIndex ==1){//确定
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
}
效果图如下,已DD为例: