3

Currently my app has an option to share on Facebook and Twitter using SLComposeViewController.

SLComposeViewController *fbComposeViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[fbComposeViewController setInitialText:text_short];
[fbComposeViewController addURL:url];
[self.navigationController presentViewController:fbComposeViewController
                                            animated:YES
                                          completion:^{
                                              NSLog(@"fb activity completed");
                                          }];

I can share to other sites like Gmail, Whatsapp, Message and Mail using

UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:@[text, url]
                                                                             applicationActivities:nil];

controller.excludedActivityTypes = @[UIActivityTypePostToWeibo,
                                         UIActivityTypePrint,
                                         UIActivityTypeCopyToPasteboard,
                                         UIActivityTypeAssignToContact,
                                         UIActivityTypeSaveToCameraRoll,
                                         UIActivityTypeAddToReadingList,
                                         UIActivityTypePostToFlickr,
                                         UIActivityTypePostToVimeo,
                                         UIActivityTypePostToTencentWeibo,
                                         UIActivityTypeAirDrop,
                                         UIActivityTypePostToFacebook,
                                         UIActivityTypePostToTwitter
                                         ];
[self presentViewController:controller animated:YES completion:nil];

However this launches a share sheet where user has to select an app he has to launch. Is there any way to directly launch the sharing dialog by specifying the activity name? For example, Whatsapp, Email and text invites in Uber.

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
Ajax
  • 1,689
  • 4
  • 20
  • 29
  • I partially understand your question , but you dont need to show the facebook and twiter in your activity controller – Anbu.Karthik Sep 19 '15 at 11:48
  • UIactivitycontroller provide to share the event whatever your application install in the app, if you want to share the particular app use custom view else use **CanopenURL** method – Anbu.Karthik Sep 19 '15 at 11:51
  • i don't want user to again select app to launch. So once he clicks say share to whatsapp, I want to share – Ajax Sep 19 '15 at 11:53
  • Can you please share an example for canOpenURL?? – Ajax Sep 19 '15 at 11:53
  • But the user must have the app installed and user must be authenticated. You should also have a backup plan in case something goes wrong with canOpenURL method – noobsmcgoobs Sep 19 '15 at 11:56

1 Answers1

3

for example if you want to share in whatsapp

// this is your share content message
NSString * msg = @"ApplNSString YOUR MSG";


NSString * urlWhats = [NSString stringWithFormat:@"whatsapp://send?text=%@",msg];
NSURL * whatsappURL = [NSURL URLWithString:[urlWhats stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

// this is identify if whatsapp is already install your device , if yes it open the whatsapp and share the content 
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
[[UIApplication sharedApplication] openURL: whatsappURL];
} else {

// it shows the alert for no application found
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"WhatsApp not installed." message:@"Your device has no WhatsApp installed." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}

additional reference

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • for example purpose I add here , If you need anything else I modify my answer – Anbu.Karthik Sep 19 '15 at 12:05
  • Thanks! that worked. Looks like I should be able to launch email and text apps in similar fashion. Just an addition, you need to urlencode the message that you are sending. Do you know where I can find documentation for application urls? esp for email and text messaging – Ajax Sep 19 '15 at 12:15
  • @Ajax - In thirdparty app/else navigate to outside app you can get call back URL just like whatsapp://, if you want to open native function use delegate methods thats is fine – Anbu.Karthik Sep 19 '15 at 12:18
  • @Ajax - do u need any additional information friend about this – Anbu.Karthik Sep 19 '15 at 12:23