zoukankan      html  css  js  c++  java
  • 在iOS8下使用CLLocationManager定位服务需要系统授权

    最近在ios8.0使用CLLocationManager定位服务,发现老不能定位,查看设置菜单中的项也是处于未知状态.想起之前都有一个弹出框提示用户是否允许定位,这次一直没有出现了.原来ios8.0下的定位服务需要申请授权了. 具体代码如下:

     1 if ([CLLocationManager locationServicesEnabled]) {
     2 
     3   self.locationManager = [[CLLocationManager alloc] init];
     4 
     5   _locationManager.delegate = self;
     6 
     7   _locationManager.desiredAccuracy = kCLLocationAccuracyBest; //控制定位精度,越高耗电量越大。
     8 
     9   _locationManager.distanceFilter = 100; //控制定位服务更新频率。单位是“米”
    10  
    11   [_locationManager startUpdatingLocation];
    12 
    13    //在ios 8.0下要授权
    14 
    15    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    16 
    17      [_locationManager requestWhenInUseAuthorization];  //调用了这句,就会弹出允许框了.
    18 
    19  }

    注意:

       在Info.plist文件还要加上NSLocationWhenInUseUsageDescription这个key,Value可以为空,

     1 #pragma mark - CLLocationManagerDelegate
     2 
     3 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
     4 
     5   
     6 
     7     CLLocation * currLocation = [locations lastObject];
     8 
     9  
    10 
    11     NSLog(@"%@",[NSString stringWithFormat:@"%.3f",currLocation.coordinate.latitude]);
    12 
    13     NSLog(@"%@",[NSString stringWithFormat:@"%.3f",currLocation.coordinate.longitude]);
    14 
    15 }
  • 相关阅读:
    贵有恒
    二叉树的中序遍历
    001.3或5的倍数
    静态成员的语法总结及应用-单例模式
    力扣42题(接雨水)
    算法笔记之二分查找
    素数筛算法之寻找每个数的最小素因子
    素数筛的算法感悟
    一维数组的逆序存放问题
    关于c++入门的几个基本代码之求和
  • 原文地址:https://www.cnblogs.com/jackma86/p/4982157.html
Copyright © 2011-2022 走看看