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允许的箭头方向。

相关推荐
yuyousheng2 小时前
mac 电脑复制粘贴剪切逻辑
macos
旭日跑马踏云飞4 小时前
【macOS】相关
macos
lichao89042720 小时前
MacOS 上部署 OpenClaw 的全界面操作方案
macos·docker·容器
美酒没故事°1 天前
mac电脑安装OpenClaw步骤
人工智能·macos
开发者导航3 天前
【开发者导航】多功能生成模型开发工具:Diffusers 详细介绍
人工智能·python·学习·macos·信息可视化
未名编程4 天前
Linux / macOS / Windows 一条命令安装 Node.js + npm(极限一行版大全)
linux·macos·node.js
was1724 天前
使用 Mole 进行 macOS 深度清理与系统优化
macos·系统清理·cli 工具
TheMemoryIsLost5 天前
mac系统装docker,不装docker desktop,安装colima
macos·docker·容器