您的位置:首页 > 移动开发 > Swift

swift中判断设备是否打开定位

2015-12-21 20:26 411 查看
在基于LBS的项目中很多都会判断是否打开了定位功能.从而做提醒用户打开或者提醒到哪里打开等操作...在OC时候我们这么写:

//检测是否开启定位

if ([CLLocationManager locationServicesEnabled] &&

([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized

|| [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined)) {

//定位功能可用,开始定位

}

else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied){

// NSlog("定位功能不可用,提示用户或忽略");

 }

但是在swift中这样不好用,调用方式也不同,看官方文档上这么说:



看了文档就可以看出,其实就是调用方式不同,所以我们swift就如下写:

//检测是否开启定位

if ((CLLocationManager.authorizationStatus() == CLAuthorizationStatus.Authorized) || (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.NotDetermined)) && (CLLocationManager .locationServicesEnabled()){

//定位开启了

}else if CLLocationManager.authorizationStatus() == CLAuthorizationStatus.Denied{

  //定位没有开启

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: