iOS 后台持续定位详解(支持ISO9.0以上)


#import <CoreLocation/CoreLocation.h>并实现CLLocationManagerDelegate 代理,.h文件完整代码如下:

[objc]  view plain  copy
  1. #import <UIKit/UIKit.h>  
  2. #import <CoreLocation/CoreLocation.h>  
  3.   
  4. @interface ViewController : UIViewController<CLLocationManagerDelegate>  
  5.   
  6. @end  

2.info.list文件:

右键,Add Row,添加的Key为NSLocationAlwaysUsageDescription,其它值默认,示例如下:



3.添加后台定位权限


4.ViewController.m 文件:

(1)定义一个私有CLLocationManager对象


(2)初始化并设置参数(initLocation方法),其中

locationManager.desiredAccuracy设置定位精度,有六个值可选,精度依次递减

kCLLocationAccuracyBestForNavigation 

kCLLocationAccuracyBest

kCLLocationAccuracyNearestTenMeters

kCLLocationAccuracyHundredMeters

kCLLocationAccuracyKilometer

kCLLocationAccuracyThreeKilometers


locationManager.pausesLocationUpdatesAutomatically 设置是否允许系统自动暂停定位,这里要设置为NO,刚开始我没有设置,后台定位持续20分钟左右就停止了!


(3)实现CLLocationManagerDelegate的代理方法,此方法在每次定位成功后调用:

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray*)locations;

*也可以通过实现以下方法:

- (void)locationManager:(CLLocationManager *)manager

didUpdateToLocation:(CLLocation *)newLocation

  fromLocation:(CLLocation *)oldLocation


(4)实现CLLocationManagerDelegate的代理方法,此方法在定位出错后调用:

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error


.m文件完整代码如下:
[objc]  view plain  copy
  1. #import "ViewController.h"  
  2.   
  3. @interface ViewController (){  
  4.     CLLocationManager *locationManager;  
  5.     CLLocation *newLocation;  
  6.     CLLocationCoordinate2D coordinate;  
  7. }  
  8.   
  9. @end  
  10.   
  11. @implementation ViewController  
  12.   
  13. - (void)viewDidLoad {  
  14.     [super viewDidLoad];  
  15.     [self initLocation];  
  16. }  
  17.   
  18. #pragma mark 初始化定位  
  19. -(void)initLocation {  
  20.     locationManager=[[CLLocationManager alloc] init];  
  21.     locationManager.delegate = self;  
  22.     locationManager.desiredAccuracy = kCLLocationAccuracyBest;//设置定位精度  
  23.     if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0){  
  24.         [locationManager requestAlwaysAuthorization];  
  25.     }  
  26.     // 9.0以后这个必须要加不加是不能实现后台持续定位的的

        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0) {

            locationManager.allowsBackgroundLocationUpdates = YES;

        }

  27.     if(![CLLocationManager locationServicesEnabled]){  
  28.         NSLog(@"请开启定位:设置 > 隐私 > 位置 > 定位服务");  
  29.     }  
  30.     locationManager.pausesLocationUpdatesAutomatically = NO;  
  31.     [locationManager startUpdatingLocation];  
  32.     //[locationManager startMonitoringSignificantLocationChanges];  
  33. }  
  34.   
  35. #pragma mark 定位成功  
  36. -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{  
  37.     newLocation = [locations lastObject];  
  38.     double lat = newLocation.coordinate.latitude;  
  39.     double lon = newLocation.coordinate.longitude;  
  40.     NSLog(@"lat:%f,lon:%f",lat,lon);  
  41. }  
  42.   
  43. #pragma mark 定位失败  
  44. -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{  
  45.     NSLog(@"error:%@",error);  
  46. }  
  47.   
  48.   
  49. - (void)didReceiveMemoryWarning {  
  50.     [super didReceiveMemoryWarning];  
  51.     // Dispose of any resources that can be recreated.  
  52. }  
  53.   
  54. @end  
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值