2011-07-07 24 views
2

バックグラウンドでX分ごとに位置を取得したい、NSLogでうまく動作しているスレッドを使用しています。しかし、locationManagerを呼び出すと動作しないようです。iOSのバックグラウンドでの位置の取得

これは私のコードです:アプリケーションで

- (void)threadEntryPoint:(ActualizarPosicionGPS2AppDelegate *)paramSender{ 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

    while([[NSThread currentThread] isCancelled] == NO){ 
     [NSThread sleepForTimeInterval:10.0f]; 
     if ([[NSThread currentThread] isCancelled] == NO && 
      [[UIApplication sharedApplication] backgroundTimeRemaining] != DBL_MAX) { 
      [self getLocation]; 
     } 
    } 

    [pool release]; 
} 

:didFinishLaunchingWithOptions:

[NSThread detachNewThreadSelector:@selector(threadEntryPoint:) toTarget:self withObject:self]; 

その後、私は次のことを持っている:

-(void) endTaskWidthIdentifier:(NSNumber *)paramIdentifier{ 
    UIBackgroundTaskIdentifier identifier = [paramIdentifier integerValue]; 
    [[UIApplication sharedApplication] endBackgroundTask:identifier]; 
} 
- (void)applicationDidEnterBackground:(UIApplication *)application { 
    self.backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^{ 
     NSNumber *backgroundTask = [NSNumber numberWithInteger:self.backgroundTaskIdentifier]; 
     [self performSelectorOnMainThread:@selector(endTaskWidthIdentifier:) withObject:backgroundTask waitUntilDone:YES]; 
     self.backgroundTaskIdentifier = UIBackgroundTaskInvalid; 
    }]; 

} 

そして重要なことは、ここですgetLocation:

locationManager.locationは常にnilを返し、現在の位置を取得しません。

何か間違っていますか?たぶん私は間違った概念を持っています!

ありがとうございました!

+0

CLLocationManagerDelegateメソッドを実装しましたか? – Anna

+0

どのメソッド?私はちょうどこれを持っています: - (void)locationManager didUpdateToLocation:fromLocation:。 .h – Ibaivi

+0

にCLLocationManagerDelegateを追加しました.UpdateToLocationが呼び出されましたか?また、 'didFailWithError'を追加して、CLが失敗しているかどうかを確認してください。 startUpdatingLocationを呼び出した直後に 'location'が値を持つとは思わないでください(そうであれば、キャッシュされた値になります)。 didUpdateToLocationの 'location'を読むことだけを提案してください。 – Anna

答えて

関連する問題