在 SwiftUI 中没有直接操作剪贴板的 API。通常需要通过系统框架完成:
iOS / iPadOS:使用 UIKit 的 UIPasteboard
macOS:使用 AppKit 的 NSPasteboard
SwiftUI 只是 UI 层,因此需要调用这些底层 API。
一、iOS / iPadOS 访问剪贴板
读取文本:
import UIKit
// 读取文本
let text = UIPasteboard.general.string
// 读取图片
let image = UIPasteboard.general.image
// 读取 URL
let url = UIPasteboard.general.url
写入操作:
import UIKit
// 写入文本
UIPasteboard.general.string = "Hello"
// 写入图片
UIPasteboard.general.image = UIImage(named: "test")
SwiftUI 示例:
import SwiftUI
struct ContentView: View {
@State private var text = ""
var body: some View {
VStack {
Text("Clipboard: \(text)")
Button("Paste") {
text = UIPasteboard.general.string ?? ""
}
Button("Copy") {
UIPasteboard.general.string = "SwiftUI Clipboard"
}
}
}
}
说明:
UIPasteboard.general 表示系统通用剪贴板。
.string 读取或写入文本。
还支持 .image、.url、.data 等类型。
UIPasteboard属性
UIPasteboard可以使用属性检测剪贴板是否包含某种类型的数据。
1、hasStrings:是否包含文本。
2、hasImages:是否包含图片。
3、hasURLs:是否包含 URL。
4、hasColors:是否包含颜色。
示例:
if UIPasteboard.general.hasImages {
print("剪贴板包含图片")
}
5、items:获取完整 item 数据。
6、types:获取 UTI 类型列表。
7、data(forPasteboardType:):获取指定 UTI 的数据。
8、setData(_:forPasteboardType:):写入指定 UTI 数据。
示例:
let pb = UIPasteboard.general
let data = pb.data(forPasteboardType: "public.png")
UIPasteboard数据结构
UIPasteboard内部是而是 Item 数组结构。
结构:
Pasteboard
├── Item 1
│ ├── public.text
│ ├── public.url
│
├── Item 2
│ ├── public.image
每个 Item 是一个字典:
[UTType : Data]
Apple 提供了批量 API:
UIPasteboard.general.items
例如,获取所有剪贴板信息:
for item in UIPasteboard.general.items {
print(item.keys)
}
或者,设置剪贴板:
let items: [[String: Any]] = [
["public.text": "Hello"],
["public.url": URL(string: "https://apple.com")!]
]
UIPasteboard.general.items = items
UIPasteboard 的类型系统
UIPasteboard为常见数据类型提供了便利访问属性(convenience properties),用于简化开发者操作剪贴板。
1、string,String? 类型,对应UTI的public.utf8-plain-text / public.text。
2、strings,[String]? 类型,对应UTI的public.text。
UIPasteboard.general.string = "Hello"
let text = UIPasteboard.general.string
3、image,UIImage? 类型,对应UTI的public.image。
4、images,[UIImage]? 类型,对应UTI的public.image。
UIPasteboard.general.image = UIImage(named: "test")
let image = UIPasteboard.general.image
5、url,URL? 类型,对应UTI的public. url。
6、urls,[URL]? 类型,对应UTI的public. url。
UIPasteboard.general.url = URL(string: "https://apple.com")
7、color,UIColor 类型,对应UTI的public. color。
UIPasteboard.general.color = .red
底层仍然是 UTI / UTType → Data 的结构,但这些属性会自动完成类型判断与数据转换。
二、macOS 访问剪贴板
读取文本:
import AppKit
let pasteboard = NSPasteboard.general
let text = pasteboard.string(forType: .string)
写入文本:
let pasteboard = NSPasteboard.general
pasteboard.clearContents()
pasteboard.setString("Hello World", forType: .string)
SwiftUI 示例:
import SwiftUI
import AppKit
struct ContentView: View {
@State private var text = ""
var body: some View {
VStack {
Text("Clipboard: \(text)")
Button("Paste") {
let pb = NSPasteboard.general
text = pb.string(forType: .string) ?? ""
}
Button("Copy") {
let pb = NSPasteboard.general
pb.clearContents()
pb.setString("SwiftUI Clipboard", forType: .string)
}
}
}
}
注意事项
1、iOS 14+ 读取剪贴板可能触发系统提示。

当应用读取 UIPasteboard.general 时,系统可能显示“从某某应用粘贴”的提示。
2、不要频繁读取剪贴板
Apple 将其视为潜在隐私行为。
3、最好在用户触发行为时读取
例如,按钮点击、粘贴操作。
