Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get notified on clipboard change in swift

is there any Clipboard Change Event in swift? how can i get notified when clipboard changed in iOS application thanks

like image 471
nmokkary Avatar asked Apr 08 '15 13:04

nmokkary


2 Answers

Here is a copy-able swift 5.0 version

NotificationCenter.default.addObserver(self, selector: #selector(clipboardChanged),
                                               name: UIPasteboard.changedNotification, object: nil)

And further, if you want to get the text in your clipboard in this event,

    @objc func clipboardChanged(){
        let pasteboardString: String? = UIPasteboard.general.string
        if let theString = pasteboardString {
            print("String is \(theString)")
            // Do cool things with the string
        }
    }
like image 172
Fangming Avatar answered Nov 18 '22 13:11

Fangming


You can capture UIPastedboardChangedNotification as described in this link:

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

Example: (impossible to make the code appeared correctly, I've pasted an image.

  1. Add notification to your didFinishLaunchingwithOptions call-back in AppDelegate

  2. Add function to handle when UIPastedboardChangedNotification sent to you AppDelegate

enter image description here

like image 3
Duyen-Hoa Avatar answered Nov 18 '22 12:11

Duyen-Hoa