2011-08-18 73 views
0

我想在後臺發佈數據,我希望從定期的時間間隔接收響應,以便我嘗試使用CLLocation Manager。但didUpdateToLocation方法只調用一次。 請推薦我! 輸入代碼在這裏位置背景 - 網絡連接

- (void)applicationDidEnterBackgroundUIApplication *)application 
    { 
    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; // 100 m 
    [locationManager startUpdatingLocation]; 
    UIApplication* app = [UIApplication sharedApplication]; 

    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ 
    [app endBackgroundTask:bgTask]; 
    bgTask = UIBackgroundTaskInvalid; 
    }]; 

    // Start the long-running task and return immediately. 
    dispatch_async(dispatch_get_global_queue(DISPATCH_ QUEUE_PRIORITY_DEFAULT, 0), ^{ 

    //Do the work associated with the task 
    // code for Posting Data Here 
    NSLog(@"backgroundTimeRemaining: %f", [[UIApplication sharedApplication] backgroundTimeRemaining]); 
    [app endBackgroundTask:bgTask]; 
    bgTask = UIBackgroundTaskInvalid; 
    }); 

    } 
    - (void)locationManagerCLLocationManager *)manager 

didUpdateToLocationCLLocation *)newLocation fromLocationCLLocation *)oldLocation {

的NSLog(@ 「位置更改」); } 此方法只能調用一次。

回答

0

當然,你應該很高興代表收到的信息至少有一次。沒有任何代碼會延遲應用程序在後臺執行代碼中的掛起。此類代碼也可以工作大約10分鐘,並且會被系統殺死。

如果您需要跟蹤在後臺用戶的位置,比也許你只是將​​此模式應用背景操作的Info.plistUIBackgroundModes,並添加選項位置)和你的應用程序將不會被暫停(僅在內存使用量很高的情況下)