iOS ActivityViewController使用

UIActivityViewController是iOS中用于展示一个活动列表的视图控制器,允许用户通过各种内置或第三方的应用来分享内容。以下是使用UIActivityViewController分享文本内容的示例代码:

import UIKit

class ViewController: UIViewController {

@IBAction func shareButtonTapped(_ sender: Any) {

let textToShare = "这是要分享的文本内容"

let activityViewController = UIActivityViewController(activityItems: [textToShare], applicationActivities: nil)

// 如果使用iPad,需要配置popoverController

if let popover = activityViewController.popoverPresentationController {

popover.sourceView = self.view // 设置popover的源视图,通常是一个按钮或其他视图

popover.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0) // 设置popover的源矩形

popover.permittedArrowDirections = [] // 移除所有箭头方向

}

self.present(activityViewController, animated: true, completion: nil)

}

}

在上述代码中,shareButtonTapped方法会在用户点击分享按钮时被调用。我们创建了一个UIActivityViewController实例,并将要分享的文本作为activityItems传入。如果你想分享图片或其他类型的数据,也可以将相应的数据对象加入到activityItems数组中。

如果你正在使用iPad,并且想要在Popover中显示UIActivityViewController,你需要设置popoverPresentationControllersourceViewsourceRect属性。permittedArrowDirections属性则用于设置Popover允许的箭头方向。

相关推荐
harmful_sheep1 天前
mac生效的终端查看
macos
iOS门童2 天前
macOS 应用"已损坏"无法打开?一文搞懂 Gatekeeper 与解决方案
macos
NPE~2 天前
[工具分享]Maccy —— 优雅的 macOS 剪贴板历史管理工具
macos·教程·工具·实用工具
差不多程序员2 天前
Mac安装OpenClaw-cn保姆级教程
macos
dzl843942 天前
mac 安装python
开发语言·python·macos
Bruce_Liuxiaowei2 天前
在 macOS 上通过 Docker 本地安装 OpenClaw 完整教程
macos·docker·容器·openclaw
阿捏利2 天前
详解Mach-O(十五)Mach-O __DATA_CONST
macos·ios·c/c++·mach-o
ShikiSuen2 天前
macOS 的 CpLk 中英切换卡顿的元凶在 InputMethodKit 本身
macos
xiayutian_c2 天前
如虎添翼-MacOS
macos
m0_737302582 天前
iOS IPA 安装 Plist 文件生成工具
macos·objective-c·cocoa