首先,我使用parse.com存储信息。 此代码仅在每次运行此方法时打开“地图”应用,并将用户位置保存在服务器中。
MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
[request setSource:[MKMapItem mapItemForCurrentLocation]];
[request setDestination:endingItem];
[request setTransportType:MKDirectionsTransportTypeAutomobile];
[request setRequestsAlternateRoutes:YES];
MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
if ( ! error && [response routes] > 0) {
MKRoute *route = [[response routes] objectAtIndex:0];
//route.distance = The distance
NSLog(@"total %f",route.expectedTravelTime );
int time = ceil(route.expectedTravelTime/60);
self.ETA = [@(time) stringValue];
NSLog(@"test %d",time);
NSLog(@"Total Distance (in Meters) :%0.1f",route.distance/1000);
self.distance = [@(route.distance*4899) stringValue];
// IF decline was pressed, need to fix if it's accepted
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setObject:self.distance forKey:@"dist"];
[PFCloud callFunctionInBackground:@"sendAccepted" withParameters:params block:^(id object, NSError *error) {
if (!error) {
NSLog(@"Success answer sent");
} else {
NSLog(@"Failed to push");
}
}];
}
}];
[endingItem openInMapsWithLaunchOptions:launchOptions];
}
我注意到,如果在运行此方法时地图应用程序已经打开,那么在我返回应用程序之前它不会保存用户数据。但是,如果在运行此方法之前关闭Maps应用程序,则始终将其发送到服务器。
现在问题我认为,如果地图应用程序未打开之前显然需要更多时间才能打开,从而让我的应用程序有更多时间来完成更新。如何解决这个问题,即使我的应用程序进入后台,它仍会更新位置?