get string from nspasteboard
Asked Answered
D

2

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?

Danonorwegian answered 29/5, 2011 at 11:41 Comment(1)
This could help: mobileorchard.com/…Tica
P
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);
Physoclistous answered 29/5, 2011 at 12:35 Comment(1)
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!Roden
S
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
Seale answered 14/2, 2018 at 13:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.