0

尽管应用程序在后台运行或作为后台服务运行,是否可以将有关 iPhone 位置的信息发送到单独的服务?

4

1 回答 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 回答