アプリケーションがバックグラウンドで実行されていても、バックグラウンドサービスとして実行されていても、iPhoneの場所に関する情報を別のサービスに送信することはできますか?
質問する
35 次
1 に答える
2
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;
}
ありがとう。
于 2013-02-17T19:05:07.020 に答える