iOS 8中使用CoreLocation定位

来源:互联网 发布:手机测分软件 编辑:程序博客网 时间:2024/06/01 10:28

  1、在使用CoreLocation前需要调用如下函数【iOS 8专用】:

  iOS 8对定位进行了一些修改,其中包括定位授权的方法,CLLocationManager增加了下面的两个方法:

  (1)始终允许访问位置信息

  - (void)requestAlwaysAuthorization;

  (2)使用应用程序期间允许访问位置数据

  - (void)requestWhenInUseAuthorization;

  示例如下:

  self.locationManager = [[CLLocationManager alloc]init];

  _locationManager.delegate = self;

  _locationManager.desiredAccuracy = kCLLocationAccuracyBest;

  _locationManager.distanceFilter = 10;

  [_locationManager requestAlwaysAuthorization];//添加这句

  [_locationManager startUpdatingLocation];

  

  2、在Info.plist文件中添加如下配置:

  (1)NSLocationAlwaysUsageDescription

  (2)NSLocationWhenInUseUsageDescription

  这两个键的值就是授权alert的描述,示例配置如下[勾选Show Raw Keys/Values后进行添加]:


  总结:

  iOS 8对定位进行了一些修改,其中包括定位授权的方法,CLLocationManager增加了以下两个方法:

  Added -[CLLocationManager requestAlwaysAuthorization]

  Added -[CLLocationManager requestWhenInUseAuthorization]

  在使用定位服务前需要通过上面两个方法申请授权:

  [CLLocationManager requestAlwaysAuthorization] 授权使应用在前台后台都能使用定位服务

  -[CLLocationManager requestWhenInUseAuthorization] 授权则与之前的一样

  另外,在使用这两个方法授权前,必须在info.plist中增加相应的键值( NSLocationAlwaysUsageDescription、NSLocationWhenInUseUsageDescription),这两个键的值就是授权alert的描述。



0 0
原创粉丝点击