iOS14以后,读取剪切板内容在App顶部会有小浮窗提示。
iOS16推送后,Apple进一步收紧了剪切板内容读取权限,具体讲就是,在App读取剪切板内容时会弹A alert,让用户选择是否允许粘贴
先看读取方法 1、在iOS16之前,读取剪切板内容需要用到UIPasteboard,iOS16以后使用UIPasteboard读取剪切板就会弹窗
let value = UIPasteboard.general.string
print("UIPasteboard.value:\(value)")
2、iOS16以后,新增UIPasteControl,可以用来读取剪切板内容,可以不弹窗,但是需要显示用户主动点击
#/// iOS 16 没有弹窗,不可直接获取剪切板内容
#/// 点击configPasteControl 后,剪贴板内容会被赋值给target
private func configPasteControl() {
if #available(iOS 16.0, *) {
let config = UIPasteControl.Configuration()
#// 背景色
config.baseBackgroundColor = .orange
#//图标与文字颜色
config.baseForegroundColor = .green
config.cornerStyle = .capsule
config.displayMode = .iconAndLabel
let pasteControl = UIPasteControl(configuration: config)
pasteControl.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
let textView = UITextView(frame: CGRect(x: 100, y: 250, width: 100, height: 100))
textView.backgroundColor = .green
#//textView editable关闭,pasteControl将不可点击,无法进行内容填充,
textView.isEditable = true
#//剪切板内容会被赋值到 target上,支持UITextView和UITextField,其他控件pasteControl将不可点击
pasteControl.target = textView
view.addSubview(textView)
view.addSubview(pasteControl)
}
点击Paste,剪切板内容会被赋值到绿色的textView上