收到有关swift剪贴板更改的通知

nmo*_*ary 5 clipboard swift

swift中有剪贴板更改事件吗?如何在iOS应用程序中更改剪贴板时收到通知谢谢

Fan*_*ing 9

这是一个可复制的swift 3.0版本

NotificationCenter.default.addObserver(self, selector: #selector(clipboardChanged),
                                               name: UIPasteboard.changedNotification, object: nil)
Run Code Online (Sandbox Code Playgroud)

此外,如果您想在此事件中获取剪贴板中的文本,

    @objc func clipboardChanged(){
        let pasteboardString: String? = UIPasteboard.general.string
        if let theString = pasteboardString {
            print("String is \(theString)")
            // Do cool things with the string
        }
    }
Run Code Online (Sandbox Code Playgroud)


Duy*_*Hoa 3

您可以按照此链接中的说明捕获 UIPastedboardChangedNotification:

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIPasteboard_Class/index.html#//apple_ref/c/data/UIPasteboardChangedNotification

示例:(不可能使代码正确显示,我粘贴了图像。

  1. 将通知添加到 AppDelegate 中的 didFinishLaunchingwithOptions 回调中

  2. 添加函数来处理 UIPastedboardChangedNotification 发送给您 AppDelegate 的情况

在此输入图像描述