iOS 後臺持續定位詳解(支持ISO9.0以上)

iOS 後臺持續定位詳解(支持ISO9.0以上)

#import <CoreLocation/CoreLocation.h>並實現CLLocationManagerDelegate 代理,.h文件完整代碼以下:git

 

[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,其它值默認,示例以下:xcode

 


3.添加後臺定位權限

4.ViewController.m 文件:

(1)定義一個私有CLLocationManager對象app

 

(2)初始化並設置參數(initLocation方法),其中oop

locationManager.desiredAccuracy設置定位精度,有六個值可選,精度依次遞減spa

 

kCLLocationAccuracyBestForNavigation .net

kCLLocationAccuracyBest代理

kCLLocationAccuracyNearestTenMetersrest

kCLLocationAccuracyHundredMeterscode

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  
相關文章
相關標籤/搜索