2012-06-13 35 views
0

我在位置管理器init中調用startMonitoringSignificantLocationChanges。在後臺模式下或在應用程序終止後處理位置事件didFinishLaunchingWithOptions

我希望如果有位置事件didFinishLaunchingWithOptions將被啓動並執行下面的代碼。 (包括背景模式,如果應用程序被終止)

該應用程序凍結(黑屏當我嘗試在不同位置的背景幾個小時後啓動它)

可能是我做錯了什麼當我得到一個地點事件。我很感激,如果有人有一個想法是什麼問題。

順便說一句,沒有辦法模擬這種位置事件,但身體移動到不同的細胞塔不同的位置。在那兒? ... :(

LocationController.m:

- (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]; 
     [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(stop) userInfo:nil repeats:NO]; 
    } 
    return self; 
} 
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; 
      }]; 
     [self updateLocationService]; 
     return YES; 
    } 
} 
- (void) updateLocationService { 
    [[LocationController sharedInstance] start]; 
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(stop) userInfo:nil repeats:NO];  
} 
- (void) stop { 
    [[LocationController sharedInstance] stop]; 
} 

謝謝

回答

2

你didFinishLaunchingWithOptions:方法如果沒有一套UIApplicationLaunchOptionsLocationKey不返回任何

只需推動迴歸您的if條款之外的聲明:

if (locationValue) 
{ 
    ... 
} 
return YES; 
相關問題