我需要一直跟踪用户位置(但不要耗尽电池)。我了解在应用程序终止后获取更新的唯一方法是使用 startMonitoringSignificantLocationChanges。
来自 Apple 关于 startMonitoringSignificantLocationChanges 的位置感知编程指南:
如果您启动此服务并且您的应用程序随后终止,则系统会在新事件到达时自动将应用程序重新启动到后台。在这种情况下,传递给 application:didFinishLaunchingWithOptions: 应用程序委托方法的选项字典包含键 UIApplicationLaunchOptionsLocationKey 以指示您的应用程序是由于位置事件而启动的。重新启动后,您仍然必须配置位置管理器对象并调用此方法以继续接收位置事件。当您重新启动位置服务时,当前事件会立即传递给您的委托。此外,即使在您启动位置服务之前,您的位置管理器对象的位置属性也会填充最新的位置对象。
如果有人可以在代码中演示(举个例子)我应该使用哪些方法,我会很高兴
在下面的代码中,我正在尝试: - 在 appdelegate 启动位置管理器,该管理器对显着的监视器更改更新和启动日期进行分析。- 在 didUpdateToLocation 中,我正在调用 stopupdating - 在 didFinishLaunchingWithOptions 中,当我检查是否有 UIApplicationLaunchOptionsLocationKey 以了解我是否在后台并由于重要的监视器位置更新而启动时。- 如果是这样,我再次调用 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");
}
谢谢你。