gpt4 book ai didi

ios - 如何检查剪贴板中是否有内容

转载 作者:搜寻专家 更新时间:2023-11-01 06:01:41 24 4
gpt4 key购买 nike

如何检查剪贴板中是否有内容?

如果剪贴板上有东西,则执行某个 Action ,如果没有,则执行另一个 Action (如示例代码所示)

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)
}

最佳答案

从 iOS 10 开始,您应该使用 UIPasteboard 属性(如 hasStrings)来检查粘贴板上是否存在特定数据类型:

var hasStrings: Bool

Returns a Boolean value indicating whether or not the strings property contains a nonempty array.


来自文档:

Starting in iOS 10, the UIPasteboard class provides properties for directly checking whether specific data types are present on a pasteboard, described in Checking for Data Types on a Pasteboard. Use these properties, rather than attempting to read pasteboard data, to avoid causing the system to needlessly attempt to fetch data before it is needed or when the data might not be present. For example, you can use the new hasStrings property to determine whether to present a string-data Paste option in the user interface, using code like this:

if UIPasteboard.general.hasStrings {
// Enable string-related control...
if let string = UIPasteboard.general.string {
// use the string here
}
}

还有一些属性可以检查数据类型;

var hasColors: Bool

Returns a Boolean value indicating whether or not the colors property contains a nonempty array.


var hasImages: Bool

Returns a Boolean value indicating whether or not the images property contains a nonempty array.


var hasURLs: Bool

Returns a Boolean value indicating whether or not the urls property contains a nonempty array.

关于ios - 如何检查剪贴板中是否有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48130383/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com