0
儘管應用程序在後臺運行或作爲後臺服務運行,是否可以將有關iPhone位置的信息發送到單獨的服務?將位置信息提交給iPhone的單獨服務?
儘管應用程序在後臺運行或作爲後臺服務運行,是否可以將有關iPhone位置的信息發送到單獨的服務?將位置信息提交給iPhone的單獨服務?
您需要在CLLocationManagerDelegate方法中調用webservice,請參閱低於
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
CLLocationCoordinate2D coordinate = [newLocation coordinate];
CLLocationCoordinate2D oldCoordinate = [oldLocation coordinate];
CLLocation *newlocation = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
CLLocation *oldlocation = [[CLLocation alloc] initWithLatitude:oldCoordinate.latitude longitude:oldCoordinate.longitude];
latitude =coordinate.latitude;
longitude =coordinate.longitude;
CLLocationDistance distDifference = [newlocation distanceFromLocation:oldlocation];
int distance = distDifference;
if (distance>0.5) {
if (isInternetReachable==TRUE) {
[self callWebserviceSetlocationInBackground:newLocation];
}
}
[newlocation release];
[oldlocation release];
}
Also you need to set "Required background modes" Flag to "App registers for location updates" to use core location updated in background.
http://i.stack.imgur.com/WtcCm.png
This may help you.
只需通過以下方法更改調用Web服務代碼方法。我希望它能起作用: -
-(void) callWebserviceSetlocationInBackground:(CLLocation *)location
{
// REMEMBER. We are running in the background if this is being executed.
// We can't assume normal network access.
// bgTask is defined as an instance variable of type UIBackgroundTaskIdentifier
// Note that the expiration handler block simply ends the task. It is important that we always
// end tasks that we have started.
UIApplication *app = [UIApplication sharedApplication];
UIBackgroundTaskIdentifier bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ [app endBackgroundTask:bgTask];}];
NSLog(@"call web service in background lati = %f and long =%f",location.coordinate.latitude,location.coordinate.longitude);
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
謝謝。