2015-04-08 28 views

回答

2

您可以在这里的链接中捕捉UIPastedboardChangedNotification:

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

例子:(不可能让代码出现正确的,我VE粘贴图像。

  1. 新增通知您didFinishLaunchingwithOptions回拨在AppDelegate中

  2. 添加函数来处理时UIPastedboardChangedNotification发送到您的appdelegate

enter image description here

+0

我已更新我的答案。 –

+0

此代码在我的应用程序中工作,但如果我复制文本的应用程序它不工作。请帮助我 – nmokkary

+0

当您的应用程序处于后台时,您不能使用NSNotification,在这种情况下,您必须每隔n秒验证一次粘贴板的内容。看看这个:http://www.ciiycode.com/0BNNmgejxqUq/grabbing-the-uipasteboard-like-pastebot-while-running-in-the-background –

2

这里有一个副本,能够SWIFT 3.0版本

NotificationCenter.default.addObserver(self, selector: #selector(clipboardChanged), 
             name: NSNotification.Name.UIPasteboardChanged , object: nil) 

而且进一步,如果你想要在此事件中获得剪贴板中的文字,

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