2013-04-02 73 views
2

我的应用需要在后台使用位置服务。所以我说根据apple's文件下面的方法无法在后台获取位置更改通知

- (void)startStandardUpdates 
{ 
    // Create the location manager if this object does not 
    // already have one. 
    if (nil == locationManager) 
     locationManager = [[CLLocationManager alloc] init]; 

    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer; 

    // Set a movement threshold for new events. 
    locationManager.distanceFilter = 500; 

    [locationManager startUpdatingLocation]; 
} 

,它说,当有关于位置变化的应用程序将得到通知,即- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation的委托方法将被调用。

中序作出一些后台处理(程度的处理时间)我已经在上述指定的委托方法内加入ExpirationHandler如下

- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 
    UIApplication *app = [UIApplication sharedApplication]; 
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ 
     bgTask = UIBackgroundTaskInvalid; 
    }]; 

    longitude = newLocation.coordinate.longitude; 
    latitude = newLocation.coordinate.latitude; 

    //Making webservice 
    [self updateServerWithLongitude:longitude andLatitude:latitude]; 
} 

并完成Web服务请求后,我已经停止后台任务如下

[[UIApplication sharedApplication] endBackgroundTask:bgTask]; 
bgTask = UIBackgroundTaskInvalid; 

预期每一件事情就可以了(能得到位置变更通知)如果我没有完成Web服务请求后停止后台任务。但如果我已完成请求后停止后台任务,我无法获取位置更改通知和状态栏中的位置服务图标消失,而应用程序在后台.....

我已经搜索了很多修复问题......这是正确的方式来获取位置更改,并在后台更新服务器的变化?如果不是,任何人都可以提出一个更好的方法来做到这一点任何帮助是极大的赞赏.....

回答

1

如果您希望在应用程序是在后台进行的位置变化的更新,苹果要求你(在这种情况下位置)指定应用程序的背景模式在info.plist中。

他们键,您需要使用是UIBackgroundModes

+0

感谢您的善意回复,但没有关于苹果的关于“位置感知编程指南”文档中启用“UIBackgroundModes”点。我的应用程序被拒绝是因为在plist中启用'UIBackgroundModes'....任何想法? –

+1

如果您需要应用程序在后台获取位置更改(并以某种方式处理或使用它),则需要启用背景模式。苹果拒绝带有后台模式的应用程序,只是把它放在那里(为了确保应用程序继续在bkg中运行),但不要真的使用该模式。 – lostInTransit

+0

我正在更新位置到数据库在locationManager:didUpdateToLocation:fromLOcation方法,它工作正常,如预期的..有什么其他事情要实施? –