我需要始终跟踪用户位置(但不会耗尽电池)。 我明白应用程序终止后获取更新的唯一方法是使用startMonitoringSignificantLocationChanges。应用程序终止后接收位置更新
从苹果的位置感知编程指南startMonitoringSignificantLocationChanges:
如果启动该服务,并且您的应用程序随后 终止,如果一个新的事件到达时,系统会自动将重新启动应用程序到 背景。在这种情况下,选择 字典传递到应用程序中:didFinishLaunchingWithOptions:您的应用程序委托的 方法中包含的关键 UIApplicationLaunchOptionsLocationKey以表明您 应用程序是因为定位事件的启动。 重新启动后,您仍然必须配置一个位置管理器对象,并调用此方法继续接收位置事件。当您重新启动 位置服务时,当前事件将立即传送到您的代理 。此外,在开始位置服务之前,您的位置 管理员对象的位置属性将使用最新的位置对象(即使是 )填充。
我会很高兴,如果有人可以在代码演示了(举个例子),这方法我应该使用
在下面的代码我我特林: - 启动在的appdelegate位置管理器这使得重要的监视器更改更新和启动更新。 - 在didUpdateToLocation我打电话stopupdating - 在didFinishLaunchingWithOptions当我检查是否有一个UIApplicationLaunchOptionsLocationKey为了知道我是否在后台,并启动由于siginificant监视器位置更新。 - 如果是这样,我再次调用startMonitoringSignificantLocationChanges(不知道为什么...) 并开始一个UIBackgeoundTaskIdentifier来调用startupdating方法。
LocationController.m :
+ (LocationController*)sharedInstance {
@synchronized(self) {
if (sharedCLDelegate == nil) {
[[self alloc] init];
}
}
return sharedCLDelegate;
}
- (id)init
{
self = [super init];
if (self != nil) {
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
[self.locationManager startUpdatingLocation];
[self.locationManager startMonitoringSignificantLocationChanges];
}
return self;
}
- (void) startMonitoringSignificantLocationChanges
{
[self.locationManager startMonitoringSignificantLocationChanges];
}
- (void) stopMonitoringSignificantLocationChanges
{
[self.locationManager stopMonitoringSignificantLocationChanges];
}
-(void) start{
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation{
if (abs([newLocation.timestamp timeIntervalSinceDate: [NSDate date]]) < 30) {
self.lastLocation = newLocation;
[self updateLocation]; //sending location to server
[self.locationManager stopUpdatingLocation];
}
}
- (void)locationManager:(CLLocationManager*)manager
didFailWithError:(NSError*)error{
[self.locationManager stopUpdatingLocation];
}
AppDelegate.h :
@interface AppDelegate : NSObject <UIApplicationDelegate> {
UIBackgroundTaskIdentifier bgTask;
}
AppDelegate.m :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
id locationValue = [launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey];
if (locationValue) {
[[LocationController sharedInstance] startMonitoringSignificantLocationChanges];
UIApplication *app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
[[LocationController sharedInstance] start]; //startupdating
return YES;
}
else {
[[LocationController sharedInstance] init];
}
}
-(void) applicationDidEnterBackground:(UIApplication *) application
{
NSLog(@"entered background Mode");
}
-(void) applicationDidBecomeActive:(UIApplication *) application
{
NSLog(@"application Did Become Active");
}
谢谢。
我想看到的这个记录在苹果的文档 – hariszaman