iOS-在后台仍然处理定位服务CLLocationManagerDelegate

来源:互联网 发布:300英雄mac版下载 编辑:程序博客网 时间:2024/06/05 20:44
当我们在程序中,使用来进行iPhone等iOS设备的定位信息功能时,我们需要知道如下的情况:

     当程序在前台运行时,我们可以从一个 CLLocationManager实例获得委托消息,在 iOS检测到设备移动到新位置时告诉你这一变化。
     当我们的程序被送到后台并且不再处于活动状态,这些位置委托消息一般是不会被发给你的程序的。实际上,他们会在你的程序回到前台后,批量的被发送。
     这样的话,如果我们需要在程序退到后台,但是,仍然能够在我们地理位置变化时,收到位置变化信息,该如何做呢?

整理一下思路:

1.在plist文件中设置 UIBackgroundModes项,并且,将值设置为: audio

2.在h文件中,实例化CLLocationManager对象,并创建一个标识程序是否在后台的BOOL值

@property (nonatomic,strong) CLLocationManager*myLocationManager;//定位管理

@property (nonatomic,unsafe_unretained, getter=isExecutingInBackground)BOOLexecutingInBackground;//判断程序是否在后台


3.在m文件中,实例化相应的对象和变量。

//定位管理

@synthesizemyLocationManager;

@synthesizeexecutingInBackground;


// 程序开启时,执行该方法

-(BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

   // 定位管理

   self.myLocationManager= [[CLLocationManageralloc]init];//初始化

   [self.myLocationManagersetDesiredAccuracy:kCLLocationAccuracyBest];//设置精度值

   [self.myLocationManagersetDelegate:self];//设置代理

   [self.myLocationManagerstartUpdatingLocation];

    

   return YES;

}


//当应用程序掉到后台时,执行该方法

 

- (void)applicationDidEnterBackground:(UIApplication*)application

{

    

  //当程序退到后台时,进行定位的重新计算

  self.executingInBackground= YES;

   [self.myLocationManagersetDesiredAccuracy:kCLLocationAccuracyHundredMeters];

  

}

 


//当应用程序回到前台时,执行该方法

-(void)applicationWillEnterForeground:(UIApplication*)application

{

  //程序进入前台,转化为高精确定位

   self.executingInBackground= NO;

   [self.myLocationManagersetDesiredAccuracy:kCLLocationAccuracyBest];

    

}


解释:

1. setDesiredAccuracy方法,用来设置CLLocationManager的定位的精确度。
定位的精确度可以分为:

externconst CLLocationAccuracykCLLocationAccuracyBest;

externconst CLLocationAccuracykCLLocationAccuracyNearestTenMeters;

externconst CLLocationAccuracykCLLocationAccuracyHundredMeters;

externconst CLLocationAccuracykCLLocationAccuracyKilometer;

externconst CLLocationAccuracykCLLocationAccuracyThreeKilometers;


2. startUpdatingLocation方法,用来根据配置来更新程序的定位服务
这个方法是同步的,会马上返回所在的地理位置信息。通过实现CLLocationManagerDelegate协议的方法。
并且,默认状态下,如果程序在后台,那么地理位置信息不会发给代理。需要在plist文件中设置UIBackgroundModes属性。(这也就是我们第一步设置的)。

3.我们设置了代理:
 [self.myLocationManager setDelegate:self];// 设置代理

所以,我们要显示的实现CLLocationManagerDelegate代理的相关方法:

#pragma mark - 判断是否在后台


//判断是否在后台

-(BOOL)isExecutingInBackground{

   return executingInBackground;

}



#pragma mark - CLLocationManagerDelegate


//定位成功时,调用该方法-iOS 5 或者之前版本

-(void)locationManager:(CLLocationManager *)managerdidUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation *)oldLocation{

   if([self isExecutingInBackground]){

      //在后台

    } else {

      //在前台

    }

}


// 定位成功时,调用该方法-iOS6.0

-(void)locationManager:(CLLocationManager *)managerdidUpdateLocations:(NSArray*)locations{

    

   if([self isExecutingInBackground]){

      //在后台

    } else {

      //在前台

    }

}


//当定位失败后,执行该方法

-(void)locationManager:(CLLocationManager *)managerdidFailWithError:(NSError*)error{


   NSLog(@"无法获得定位信息");


}


希望对你有所帮助!
0 0
原创粉丝点击