在Swift中用UIActivityViewController共享文本或图像的基本示例

我开始我的search,想知道如何在iOS中分享给其他应用程序。 我发现有两个重要的方法

  • UIActivityViewController
  • UIDocumentInteractionController

这个和其他的方法在这个答案中比较。

通常当我正在学习一个新的概念时,我喜欢看到一个让我开始的基本例子。 一旦我得到了一些基本的设置,我可以修改它,如何我喜欢以后。

有许多与UIActivityViewController相关的SO问题,但我找不到任何只是要求一个简单的例子。 由于我刚刚学会了如何做到这一点,我将在下面提供自己的答案。 随意添加一个更好的(或Objective-C版本)。

UIActivityViewController示例项目

更新了Swift 3

用两个button设置你的故事板,并把它们连接到你的视图控制器(见下面的代码)。

在这里输入图像说明

将图像添加到您的Assets.xcassets。 我叫我的“狮子”。

在这里输入图像说明

 import UIKit class ViewController: UIViewController { // share text @IBAction func shareTextButton(_ sender: UIButton) { // text to share let text = "This is some text that I want to share." // set up activity view controller let textToShare = [ text ] let activityViewController = UIActivityViewController(activityItems: textToShare, applicationActivities: nil) activityViewController.popoverPresentationController?.sourceView = self.view // so that iPads won't crash // exclude some activity types from the list (optional) activityViewController.excludedActivityTypes = [ UIActivityType.airDrop, UIActivityType.postToFacebook ] // present the view controller self.present(activityViewController, animated: true, completion: nil) } // share image @IBAction func shareImageButton(_ sender: UIButton) { // image to share let image = UIImage(named: "Image") // set up activity view controller let imageToShare = [ image! ] let activityViewController = UIActivityViewController(activityItems: imageToShare, applicationActivities: nil) activityViewController.popoverPresentationController?.sourceView = self.view // so that iPads won't crash // exclude some activity types from the list (optional) activityViewController.excludedActivityTypes = [ UIActivityType.airDrop, UIActivityType.postToFacebook ] // present the view controller self.present(activityViewController, animated: true, completion: nil) } } 

结果

点击“分享一些文字”给左边的结果,点击“分享图像”给出右边的结果。

在这里输入图像说明

笔记

  • 如果您希望隐藏其中一些选项,您可以使用excludedActivityTypes来完成,如上面的代码所示。
  • 不包括popoverPresentationController?.sourceView行会导致您的应用在iPad上运行时崩溃。
  • 这不允许你分享文字或图像到其他应用程序。 你可能想要UIDocumentInteractionController

也可以看看

  • 通过UIActivityViewController添加共享到你的Swift应用程序
  • UIActivity从NSHipster 查看控制器
  • UIActivityViewController文档
  • 共享扩展文档
  • UIDocumentInteractionController比较

就像一个笔记,你也可以使用这个iPad的:

 activityViewController.popoverPresentationController?.sourceView = sender 

所以popover从发件人(这种情况下的button)popup。

如果你想分享整个屏幕,我发现这个工作完美无瑕。

 @IBAction func shareButton(_ sender: Any) { let bounds = UIScreen.main.bounds UIGraphicsBeginImageContextWithOptions(bounds.size, true, 0.0) self.view.drawHierarchy(in: bounds, afterScreenUpdates: false) let img = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() let activityViewController = UIActivityViewController(activityItems: [img!], applicationActivities: nil) activityViewController.popoverPresentationController?.sourceView = self.view self.present(activityViewController, animated: true, completion: nil) } 

您可以使用我在一个项目的助手类中编写的以下函数。

只是打电话

 showShareActivity(msg:"message", image: nil, url: nil, sourceRect: nil) 

它将适用于iPhone和iPad。 如果你通过sourceRect传递任何视图的CGRect值,它也会在iPad中显示一个小箭头。

 func topViewController()-> UIViewController{ var topViewController:UIViewController = UIApplication.shared.keyWindow!.rootViewController! while ((topViewController.presentedViewController) != nil) { topViewController = topViewController.presentedViewController!; } return topViewController } func showShareActivity(msg:String?, image:UIImage?, url:String?, sourceRect:CGRect?){ var objectsToShare = [AnyObject]() if let url = url { objectsToShare = [url as AnyObject] } if let image = image { objectsToShare = [image as AnyObject] } if let msg = msg { objectsToShare = [msg as AnyObject] } let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil) activityVC.modalPresentationStyle = .popover activityVC.popoverPresentationController?.sourceView = topViewController().view if let sourceRect = sourceRect { activityVC.popoverPresentationController?.sourceRect = sourceRect } topViewController().present(activityVC, animated: true, completion: nil) }