iOS设备定位服务开启判定

应用CLLocationManager 的两个方法

  • [CLLocationManagerlocationServicesEnabled] 判断设备是否开启定位功能

  • [CLLocationManagerauthorizationStatus] 判断设备是否允许本程序的定位服务

- (BOOL)isOnLocation
{
    BOOL isOn = false;
    if (([CLLocationManager locationServicesEnabled]) && ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized)) {
        NSLog(@"定位已经开启");
        isOn = true;
    } else {
        NSLog(@"定位未开启");
        isOn = false;
    }
    return isOn;
}

使用:

    if (![self isOnLocation]) {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"警告" message:@"您的定位服务没有打开,请在设置中开启" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        //[[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"prefs:root=LOCATION_SERVICES"]];

    }

当服务关闭时:

posted @ 2013-11-13 09:51  cocoajin  阅读(481)  评论(0编辑  收藏  举报