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

时间:2016-03-11 03:59:47

标签: ios swift uiactivityviewcontroller

我开始搜索时想知道如何与iOS中的其他应用分享。我发现有两个重要的方法是

  • UIActivityViewController
  • UIDocumentInteractionController

this SO answer中比较这些和其他方法。

通常,当我学习一个新概念时,我希望看到一个让我入门的基本例子。一旦我得到基本的设置,我可以稍后修改它。

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

6 个答案:

答案 0 :(得分:233)

UIActivityViewController示例项目

使用两个按钮设置故事板并将它们连接到视图控制器(参见下面的代码)。

enter image description here

将图像添加到Assets.xcassets。我打电话给我的狮子"。

enter image description here

代码

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)
    }

}

结果

点击"分享一些文字"在左侧给出结果并单击"分享图像"在右边给出结果。

enter image description here

注释

  • 我用iOS 11和Swift 4重新测试了这个。我必须在它运行之前在模拟器中运行它几次因为它超时了。这可能是因为我的电脑很慢。
  • 如果您希望隐藏其中一些选项,可以使用excludedActivityTypes执行此操作,如上面的代码所示。
  • 不包括popoverPresentationController?.sourceView行会导致您的应用在iPad上运行时崩溃。
  • 这不允许您与其他应用共享文字或图片。你可能想要UIDocumentInteractionController

另见

答案 1 :(得分:45)

分享文字

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

分享图片

@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)
 }

enter image description here

答案 2 :(得分:7)

如果你想分享整个屏幕,我发现这可以完美地运作。

@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)
}

答案 3 :(得分:5)

正如笔记一样,您也可以将它用于iPad:

activityViewController.popoverPresentationController?.sourceView = sender

因此弹出窗口从发件人弹出(在这种情况下是按钮)。

答案 4 :(得分:2)

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

只需致电

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)
}

答案 5 :(得分:1)

我使用了上面的实现,现在我才知道它在运行iOS 13的iPad上不起作用。 我必须在present()调用之前添加这些行,以使其正常工作

client.on('ready', function(){
    setInterval(checkEntries, 500);
});

这就是我的工作方式

//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)
}