2013-11-23 48 views
0

我试图跟踪应用程序移动到背景时的用户位置。我只需要跟踪一次。所以,我没有使用后台定位服务。在本文下面的代码工作时,我去掉1,但是当我取消2在iOS 7中后台跟踪用户位置

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    // 1 works here [self startStandardUpdates]; 

    self.bgTask = [application beginBackgroundTaskWithExpirationHandler:^{ 

    [application endBackgroundTask:self.bgTask]; 
    self.bgTask = UIBackgroundTaskInvalid; 
}]; 

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{ 

    // 2 doesn't work here [self startStandardUpdates]; 

}); 

} 

在第二种情况下该委托函数不叫这是行不通的。

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 
{ 
    NSLog(@"Updated\n%@",locations); 
    [self.manager stopUpdatingLocation]; 
    [[UIApplication sharedApplication] endBackgroundTask:self.bgTask]; 
    self.bgTask = UIBackgroundTaskInvalid; 

} 

有人能告诉我为什么这不是在第二种情况下工作。另外,在第一个位置使用[self startStandardUpdates]是否正确?

+0

你可以发布''startStandardUpdates''方法吗? – initWithQuestion

+0

@initWithQuestion对不起,很久以前我问过这个问题。我没有访问代码...感谢您的兴趣 – aMother

回答

0

试试这个

默认情况下,这是针对iOS 6.0以上版本联动应用。

if ([self.manager respondsToSelector:@selector(pausesLocationUpdatesAutomatically)]) { 
    self.manager.pausesLocationUpdatesAutomatically = NO; 
} 
+0

它没有工作。 – aMother