How to exit from Map App in IOS 6 i.e. return cont

2019-08-13 14:16发布

问题:

I am using MKMapItem to launch Map app from within my app after the user taps 'Directions' button. The Map app comes up fine showing directions from current location to a address. But how do I go back to my app, If I no longer want to see Directions? Below is my code attached to IBAction.

Code:


- (IBAction)pressDirectionsButton:(id)sender {

Class mapItemClass = [MKMapItem class];
if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)])
{
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    NSString *addressString = [NSString stringWithFormat:@"%@,%@,%@,%@,%@",
                               self.currentlySelectedTemple.houseNumber,
                               self.currentlySelectedTemple.street,
                               self.currentlySelectedTemple.city,
                               self.currentlySelectedTemple.state,
                               self.currentlySelectedTemple.country];
    [geocoder geocodeAddressString:addressString completionHandler:^(NSArray *placemarks, NSError *error) {

        //Convert CLPlacemark to an MKPlacemark
        CLPlacemark *geocodedPlacemark = [placemarks objectAtIndex:0];
        MKPlacemark *placemark = [[MKPlacemark alloc]
                                  initWithCoordinate:geocodedPlacemark.location.coordinate addressDictionary:geocodedPlacemark.addressDictionary];

        //Create a map item for geocoded address to pass to Maps app
        MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
        [mapItem setName:geocodedPlacemark.name];

        //Set Directions mode to Driving
        NSDictionary *launchOptions =    @{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving};

        //Get the "Current User Locations" MKMapItem
        MKMapItem *currentLocationMapItem = [MKMapItem mapItemForCurrentLocation];

        //Pass the current location and destination  map items to Maps app
        [MKMapItem openMapsWithItems:@[currentLocationMapItem,  mapItem] launchOptions:launchOptions];

       }];
    }

    }

回答1:

There is no way to go back to your app (programatically), as you are no longer in your app, but in the Maps app. After executing openMapsWithItems:launchOptions:, your App Delegate's applicationdDidEnterBackground: method is called.

Currently there is no way to use Mapkit to embed a map with directions within your app.