17

maybe it's been a long night but I am not able to understand how to check the clipboard for strings

I have been reading the NSPasteboard documentation..

could some one help me out?

BlockReader
  • 641
  • 1
  • 8
  • 17
  • 1
    This could help: http://www.mobileorchard.com/new-in-iphone-30-tutorial-series-part-3-copy-paste-with-uipasteboard/ – Luke May 29 '11 at 12:24

2 Answers2

33

you need to use the following method with stringForType with key NSPasteboardTypeString to read the string value from clipboard.

- (NSString *)stringForType:(NSString *)dataType .

NSPasteboard*  myPasteboard  = [NSPasteboard generalPasteboard];
NSString* myString = [myPasteboard  stringForType:NSPasteboardTypeString];

To do this for iOS with UIPasteBoard use the following code:

UIPasteboard *thePasteboard = [UIPasteboard generalPasteboard];
NSString *pasteboardString = thePasteboard.string;
NSLog(@"%@", pasteboardString);
Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195
Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76
  • While NSPasteboard is available in OS X v10.0 and later, there are some issues using NSPasteboardTypeString in OS X v10.5 and earlier. In v10.6, many Pasteboard types with have new UTIs and the constants changed. This is probably ancient history, but worth mentioning. Great answer, thank you! – Cory Trese Oct 23 '14 at 15:05
4

You can find the Swift 4 version on below for both Mac and iOS.

Mac

    let pasteboard = NSPasteboard.general
    let copiedString = pasteboard.string(forType: .string)

iOS

    let pasteboard = UIPasteboard.general
    let copiedString = pasteboard.string // might be nil value, is an optional variable
abdullahselek
  • 7,893
  • 3
  • 50
  • 40