在 Swift 中使用 UIActivityViewController 共享文本或图像的基本示例
Basic example for sharing text or image with UIActivityViewController in Swift
我开始搜索是想知道如何分享到 iOS 中的其他应用程序。我发现两个重要的方法是
UIActivityViewController
UIDocumentInteractionController
在 this SO answer 中比较了这些方法和其他方法。
通常当我学习一个新概念时,我喜欢看一个基本的例子来帮助我入门。一旦我完成了一些基本设置,我就可以稍后根据自己的喜好对其进行修改。
有很多与 UIActivityViewController
相关的 SO 问题,但我找不到任何只是要求一个简单示例的问题。由于我刚刚学会了如何执行此操作,因此我将在下面提供自己的答案。欢迎添加更好的版本(或 Objective-C 版本)。
UIActivityViewController 示例项目
使用两个按钮设置故事板并将它们连接到视图控制器(请参阅下面的代码)。
将图片添加到您的 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 = [ UIActivity.ActivityType.airDrop, UIActivity.ActivityType.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 = [ UIActivity.ActivityType.airDrop, UIActivity.ActivityType.postToFacebook ]
// present the view controller
self.present(activityViewController, animated: true, completion: nil)
}
}
结果
点击“分享一些文字”在左侧给出结果,点击“分享图片”在右侧给出结果。
备注
- 我用 iOS 11 和 Swift 4 重新测试了这个。我不得不在模拟器中 运行 它几次才能工作,因为它超时了。这可能是因为我的电脑速度慢。
- 如果您希望隐藏其中一些选项,您可以使用
excludedActivityTypes
来实现,如上面的代码所示。
- 不包含
popoverPresentationController?.sourceView
行将导致您的应用程序在 运行 iPad 时崩溃。
- 这不允许您与其他应用共享文本或图像。你可能想要
UIDocumentInteractionController
。
另见
- Add sharing to your Swift app via UIActivityViewController
- UIActivityViewController 来自 NSHipster
UIActivityViewController
documentation
- Share extension documentation
- comparison with
UIDocumentInteractionController
请注意,您也可以将此用于 iPad:
activityViewController.popoverPresentationController?.sourceView = sender
因此弹出窗口从发件人处弹出(在这种情况下是按钮)。
如果你想共享整个屏幕,我发现这个可以完美地工作。
@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)
}
你可以在项目中使用我在我的一个助手class中编写的以下功能。
只要打电话
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)
}
分享:文字
@IBAction func shareOnlyText(_ sender: UIButton) {
let text = "This is the text....."
let textShare = [ text ]
let activityViewController = UIActivityViewController(activityItems: textShare , applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
self.present(activityViewController, animated: true, completion: nil)
}
}
分享: 图片
@IBAction func shareOnlyImage(_ sender: UIButton) {
let image = UIImage(named: "Product")
let imageShare = [ image! ]
let activityViewController = UIActivityViewController(activityItems: imageShare , applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
self.present(activityViewController, animated: true, completion: nil)
}
分享:文字-图片-URL
@IBAction func shareAll(_ sender: UIButton) {
let text = "This is the text...."
let image = UIImage(named: "Product")
let myWebsite = NSURL(string:"https://whosebug.com/users/4600136/mr-javed-multani?tab=profile")
let shareAll= [text , image! , myWebsite]
let activityViewController = UIActivityViewController(activityItems: shareAll, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
self.present(activityViewController, animated: true, completion: nil)
}
我已经使用了上面的实现,现在我才知道它不适用于 iPad 运行 iOS 13。
我必须在 present() 调用之前添加这些行才能使其工作
//avoiding to crash on iPad
if let popoverController = activityViewController.popoverPresentationController {
popoverController.sourceRect = CGRect(x: UIScreen.main.bounds.width / 2, y: UIScreen.main.bounds.height / 2, width: 0, height: 0)
popoverController.sourceView = self.view
popoverController.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
}
这就是我的工作方式
func shareData(_ dataToShare: [Any]){
let activityViewController = UIActivityViewController(activityItems: dataToShare, applicationActivities: nil)
//exclude some activity types from the list (optional)
//activityViewController.excludedActivityTypes = [
//UIActivity.ActivityType.postToFacebook
//]
//avoiding to crash on iPad
if let popoverController = activityViewController.popoverPresentationController {
popoverController.sourceRect = CGRect(x: UIScreen.main.bounds.width / 2, y: UIScreen.main.bounds.height / 2, width: 0, height: 0)
popoverController.sourceView = self.view
popoverController.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
}
self.present(activityViewController, animated: true, completion: nil)
}
我开始搜索是想知道如何分享到 iOS 中的其他应用程序。我发现两个重要的方法是
UIActivityViewController
UIDocumentInteractionController
在 this SO answer 中比较了这些方法和其他方法。
通常当我学习一个新概念时,我喜欢看一个基本的例子来帮助我入门。一旦我完成了一些基本设置,我就可以稍后根据自己的喜好对其进行修改。
有很多与 UIActivityViewController
相关的 SO 问题,但我找不到任何只是要求一个简单示例的问题。由于我刚刚学会了如何执行此操作,因此我将在下面提供自己的答案。欢迎添加更好的版本(或 Objective-C 版本)。
UIActivityViewController 示例项目
使用两个按钮设置故事板并将它们连接到视图控制器(请参阅下面的代码)。
将图片添加到您的 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 = [ UIActivity.ActivityType.airDrop, UIActivity.ActivityType.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 = [ UIActivity.ActivityType.airDrop, UIActivity.ActivityType.postToFacebook ]
// present the view controller
self.present(activityViewController, animated: true, completion: nil)
}
}
结果
点击“分享一些文字”在左侧给出结果,点击“分享图片”在右侧给出结果。
备注
- 我用 iOS 11 和 Swift 4 重新测试了这个。我不得不在模拟器中 运行 它几次才能工作,因为它超时了。这可能是因为我的电脑速度慢。
- 如果您希望隐藏其中一些选项,您可以使用
excludedActivityTypes
来实现,如上面的代码所示。 - 不包含
popoverPresentationController?.sourceView
行将导致您的应用程序在 运行 iPad 时崩溃。 - 这不允许您与其他应用共享文本或图像。你可能想要
UIDocumentInteractionController
。
另见
- Add sharing to your Swift app via UIActivityViewController
- UIActivityViewController 来自 NSHipster
UIActivityViewController
documentation- Share extension documentation
- comparison with
UIDocumentInteractionController
请注意,您也可以将此用于 iPad:
activityViewController.popoverPresentationController?.sourceView = sender
因此弹出窗口从发件人处弹出(在这种情况下是按钮)。
如果你想共享整个屏幕,我发现这个可以完美地工作。
@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)
}
你可以在项目中使用我在我的一个助手class中编写的以下功能。
只要打电话
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)
}
分享:文字
@IBAction func shareOnlyText(_ sender: UIButton) {
let text = "This is the text....."
let textShare = [ text ]
let activityViewController = UIActivityViewController(activityItems: textShare , applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
self.present(activityViewController, animated: true, completion: nil)
}
}
分享: 图片
@IBAction func shareOnlyImage(_ sender: UIButton) {
let image = UIImage(named: "Product")
let imageShare = [ image! ]
let activityViewController = UIActivityViewController(activityItems: imageShare , applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
self.present(activityViewController, animated: true, completion: nil)
}
分享:文字-图片-URL
@IBAction func shareAll(_ sender: UIButton) {
let text = "This is the text...."
let image = UIImage(named: "Product")
let myWebsite = NSURL(string:"https://whosebug.com/users/4600136/mr-javed-multani?tab=profile")
let shareAll= [text , image! , myWebsite]
let activityViewController = UIActivityViewController(activityItems: shareAll, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
self.present(activityViewController, animated: true, completion: nil)
}
我已经使用了上面的实现,现在我才知道它不适用于 iPad 运行 iOS 13。 我必须在 present() 调用之前添加这些行才能使其工作
//avoiding to crash on iPad
if let popoverController = activityViewController.popoverPresentationController {
popoverController.sourceRect = CGRect(x: UIScreen.main.bounds.width / 2, y: UIScreen.main.bounds.height / 2, width: 0, height: 0)
popoverController.sourceView = self.view
popoverController.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
}
这就是我的工作方式
func shareData(_ dataToShare: [Any]){
let activityViewController = UIActivityViewController(activityItems: dataToShare, applicationActivities: nil)
//exclude some activity types from the list (optional)
//activityViewController.excludedActivityTypes = [
//UIActivity.ActivityType.postToFacebook
//]
//avoiding to crash on iPad
if let popoverController = activityViewController.popoverPresentationController {
popoverController.sourceRect = CGRect(x: UIScreen.main.bounds.width / 2, y: UIScreen.main.bounds.height / 2, width: 0, height: 0)
popoverController.sourceView = self.view
popoverController.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
}
self.present(activityViewController, animated: true, completion: nil)
}