在 Swift 中与 UIActivityViewController 共享文本或图像的基本示例 [英] Basic example for sharing text or image with UIActivityViewController in Swift

查看:38
本文介绍了在 Swift 中与 UIActivityViewController 共享文本或图像的基本示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始搜索时想知道如何与 iOS 中的其他应用程序共享.我发现两个重要的方式是

  • UIActivityViewController
  • UIDocumentInteractionController

这些方法和其他方法在

将图像添加到您的 Assets.xcassets.我叫我的狮子".

代码

导入 UIKit类视图控制器:UIViewController {//分享文字@IBAction func shareTextButton(_ sender: UIButton) {//要分享的文字let text = "这是我想分享的一些文字.";//设置活动视图控制器让 textToShare = [ 文本 ]让activityViewController = UIActivityViewController(activityItems: textToShare, applicationActivities: nil)activityViewController.popoverPresentationController?.sourceView = self.view//这样 iPad 就不会崩溃//从列表中排除一些活动类型(可选)activityViewController.excludedActivityTypes = [ UIActivity.ActivityType.airDrop, UIActivity.ActivityType.postToFacebook ]//呈现视图控制器self.present(activityViewController,动画:true,完成:nil)}//分享图像@IBAction func shareImageButton(_ sender: UIButton) {//要分享的图片let image = UIImage(named: "Image")//设置活动视图控制器让 imageToShare = [ 图像!]让activityViewController = UIActivityViewController(activityItems: imageToShare, applicationActivities: nil)activityViewController.popoverPresentationController?.sourceView = self.view//这样 iPad 就不会崩溃//从列表中排除一些活动类型(可选)activityViewController.excludedActivityTypes = [ UIActivity.ActivityType.airDrop, UIActivity.ActivityType.postToFacebook ]//呈现视图控制器self.present(activityViewController,动画:true,完成:nil)}}

结果

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

注意事项

  • 我使用 iOS 11 和 Swift 4 对此进行了重新测试.我不得不在模拟器中运行几次,然后它才能工作,因为它超时了.这可能是因为我的电脑速度很慢.
  • 如果您希望隐藏其中一些选项,您可以使用 excludedActivityTypes 来实现,如上面的代码所示.
  • 不包括 popoverPresentationController?.sourceView 行会导致您的应用在 iPad 上运行时崩溃.
  • 这不允许您与其他应用共享文本或图像.您可能需要 UIDocumentInteractionController 来实现这一点.

另见

I started my search by wanting to know how I could share to other apps in iOS. I discovered that two important ways are

  • UIActivityViewController
  • UIDocumentInteractionController

These and other methods are compared in this SO answer.

Often when I am learning a new concept I like to see a basic example to get me started. Once I get something basic set up I can modify it how I like later.

There are many SO questions related to UIActivityViewController, but I couldn't find any that were just asking for a simple example. Since I just learned how to do this, I will provide my own answer below. Feel free to add a better one (or an Objective-C version).

解决方案

UIActivityViewController Example Project

Set up your storyboard with two buttons and hook them up to your view controller (see code below).

Add an image to your Assets.xcassets. I called mine "lion".

Code

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

Result

Clicking "Share some text" gives result on the left and clicking "Share an image" gives the result on the right.

Notes

  • I retested this with iOS 11 and Swift 4. I had to run it a couple times in the simulator before it worked because it was timing out. This may be because my computer is slow.
  • If you wish to hide some of these choices, you can do that with excludedActivityTypes as shown in the code above.
  • Not including the popoverPresentationController?.sourceView line will cause your app to crash when run on an iPad.
  • This does not allow you to share text or images to other apps. You probably want UIDocumentInteractionController for that.

See also

这篇关于在 Swift 中与 UIActivityViewController 共享文本或图像的基本示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆