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

时间:2016-01-22 06:36:30

标签: ios objective-c ios9 uiactivityviewcontroller

在我的应用程序中,我想像WhatsApp,Facebook,Twitter,Mail一样创建一个分享按钮。

这是我的代码

-(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图标,我想要像这样的无聊的whatsup图标消息。

3 个答案:

答案 0 :(得分:8)

试试这个

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

答案 1 :(得分:1)

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

答案 2 :(得分:0)

迅捷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)