Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i share location using UIActivityViewController?

Tags:

iphone

UIActivityViewController is a great way to share images and text. How can I share a location as well? To share images and text, I add the corresponding objects to an NSArray, which I pass then as the UIActivities. I would like to just add the CLLocationCoordinate2D, but that's a struct, not an object.

Any ideas?

like image 443
Daniel Avatar asked Jan 19 '13 04:01

Daniel


1 Answers

I've had the same problem but couldn't find an answer to getting the coordinates to work through the UIActivityViewController.

As a workaround, I used a similar approach to what I've seen being used in WhatsApp, where you get an action sheet with the different map providers. The following code will show an alert and let you select to open a particular location through Waze/Google Maps/Apple Maps - only the installed apps will show up. Just replace the "longitude" and "latitude" values with your CLLocationCoordinate2D latitude/longitude properties.

UIAlertController* alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

 UIAlertAction* appleMaps = [UIAlertAction actionWithTitle:@"Open in Maps" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://maps.apple.com/?q=%@,%@", latitude, longitude]]];
        }];
 UIAlertAction* googleMaps = [UIAlertAction actionWithTitle:@"Open in Google Maps" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"comgooglemaps://?q=%@,%@", latitude, longitude]]];
        }];
 UIAlertAction* waze = [UIAlertAction actionWithTitle:@"Open in Waze" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"waze://?ll=%@,%@", latitude, longitude]]];
        }];

[alert addAction:appleMaps];
 if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]])
            [alert addAction:googleMaps];
 if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"waze://"]])
            [alert addAction:waze];


UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:nil];

[alert addAction:cancel];

[self presentViewController:alert animated:YES completion:nil];
like image 87
migues Avatar answered Nov 19 '22 16:11

migues