如何在iOS 9中创建共享按钮?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在iOS 9中创建共享按钮?相关的知识,希望对你有一定的参考价值。

在我的应用程序中,我想创建一个分享按钮与WhatsApp,Facebook,Twitter,邮件这样。

这是我的代码

-(IBAction)share:(id)sender
{
NSString *shareText = @"Hi This is a Reward Game App";
NSArray *itemsToShare = @[shareText];
UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:itemsToShare applicationActivities:nil];
activityVC.excludedActivityTypes = @[UIActivityTypePostToWeibo,UIActivityTypePostToTwitter,UIActivityTypePostToFacebook,UIActivityTypeMail,UIActivityTypeMessage,UIActivityTypeAssignToContact,UIActivityTypePostToTencentWeibo];
[self presentViewController:activityVC animated:YES completion:nil];
}

我正在写这个代码我的模拟器显示这样,

enter image description here

它只显示邮件和Facebook图标,我想要更多这样的whatsapp图标消息。

答案

试试这个

-(IBAction)shareAction:(id)sender
 {    
        NSArray* sharedObjects=[NSArray arrayWithObjects:@"sharecontent",  nil];
        UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:sharedObjects applicationActivities:nil];
        activityViewController.popoverPresentationController.sourceView = self.view;
        [self presentViewController:activityViewController animated:YES completion:nil];
 }
另一答案

Swift 4.2

func shareMessage(message: String, link: String) {
    if let link = NSURL(string: link) {
        let objectsToShare = [message, link] as [Any]
        let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
        present(activityVC, animated: true, completion: nil)
    }
}

叫这个功能:

shareMessage(message: "Hello World!", link: "https://www.apple.com/")

输出:

enter image description here

另一答案

swift 4.2那段代码现在正在运行

        let text = "race"
        let url = "https://medium.com/@javedmultani16/share-text-image-and-url-by-using-uiactivityviewcontroller-in-swift-4eb253e43d55"
        let shareAll = [text, url] as [Any]
        let activityViewController = UIActivityViewController(activityItems: shareAll, applicationActivities: nil)
        activityViewController.popoverPresentationController?.sourceView = self.view
        self.present(activityViewController, animated: true, completion: nil)

以上是关于如何在iOS 9中创建共享按钮?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 iOS 的 xamarin 表单中创建渐变按钮背景

如何在 iOS8 的通知窗口中创建按钮?

如何在 iOS 中创建在每部 iPhone 中正确显示的按钮列表

如何使用 Swift 4 在 iOS 9+ 中创建瀑布布局

如何在 swift (iOS) 中创建单选按钮和复选框?

在片段中创建日期选择器