B2F*_*2Fq 5 clipboard ios swift
如何检查剪贴板上是否有内容?
如果剪贴板上有内容,则执行某个操作,如果没有,则执行另一个操作(如示例代码所示)
if (if in the clipboard, that is, then open the VC) {
let modalViewController = self.storyboard?.instantiateViewController(withIdentifier: "Clipboard") as? ClipboardViewController
modalViewController?.modalPresentationStyle = .overCurrentContext
self.present(modalViewController!, animated: true, completion: nil)
} else (if the clipboard is empty then) {
let alert = UIAlertController(title: "Clipboard is Empty", message: "It's recommended you bring your towel before continuing.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "No", style: .cancel, handler: nil))
self.present(alert, animated: true)
}
Run Code Online (Sandbox Code Playgroud)
从 iOS 10 开始,您应该使用诸如检查粘贴板上是否存在特定数据类型之UIPasteboard类的属性:hasStrings
var hasStrings: Bool
Run Code Online (Sandbox Code Playgroud)
返回一个布尔值,指示 strings 属性是否包含非空数组。
来自文档:
从 iOS 10 开始,UIPasteboard 类提供了用于直接检查粘贴板上是否存在特定数据类型的属性,如检查粘贴板上的数据类型中所述。使用这些属性,而不是尝试读取粘贴板数据,以避免导致系统在需要数据之前或在数据可能不存在时不必要地尝试获取数据。例如,您可以使用新的 hasStrings 属性来确定是否在用户界面中显示字符串数据粘贴选项,使用如下代码:
if UIPasteboard.general.hasStrings {
// Enable string-related control...
if let string = UIPasteboard.general.string {
// use the string here
}
}
Run Code Online (Sandbox Code Playgroud)
还有另外一些属性可以检查数据类型;
var hasColors: Bool
Run Code Online (Sandbox Code Playgroud)
返回一个布尔值,指示颜色属性是否包含非空数组。
var hasImages: Bool
Run Code Online (Sandbox Code Playgroud)
返回一个布尔值,指示 images 属性是否包含非空数组。
var hasURLs: Bool
Run Code Online (Sandbox Code Playgroud)
返回一个布尔值,指示 urls 属性是否包含非空数组。
您应该使用UIPasteboard类。
if let value = UIPasteboard.general.string {
// there is value in clipboard
} else {
// clipboard is empty
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3306 次 |
| 最近记录: |