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

在基于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{

  //定位没有开启

    }

        

posted @ 2015-12-21 20:26  CJDanger  阅读(1433)  评论(0编辑  收藏  举报