3

目前,我的应用程序可以选择使用 SLComposeViewController 在 Facebook 和 Twitter 上分享。

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

我可以分享到其他网站,例如 Gmail、Whatsapp、Message 和 Mail,使用

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];

但是,这会启动一个共享表,用户必须在其中选择他必须启动的应用程序。有没有办法通过指定活动名称直接启动共享对话框?例如,Uber 中的 Whatsapp、电子邮件和文本邀请。

4

1 回答 1

3

例如,如果你想分享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];
}

附加参考

于 2015-09-19T12:02:51.617 回答