How do I display a user's clipboard contents in an UILabel? - ios

How do I display the contents of the users clipboard in an UILabel?
Thanks!

This line would do.
UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
lbl.text = pasteBoard.string;

Related

Paste UIPasteboard contents in other apps

I used the following code to copy some texts to UIPasteboard:
[[UIPasteboard generalPasteboard] setPersistent:YES];
[UIPasteboard generalPasteboard].string = #"Testing";
And I'm able to read the content by:
NSLog(#"Pasteboard String: %#", [UIPasteboard generalPasteboard].string);
// which gives "Testing"
However, I'm unable to retrieve the copied text in any other apps. What is the proper way to copy texts in my App and then paste in other apps?
Using iOS 8.
// Save text
UIPasteboard* board = [UIPasteboard
pasteboardWithName:#"com.company.wtv" create:YES];
board.persistent=YES; [board setValue:#"123456ccc"
forPasteboardType:#"com.company.wtv.sharedValue"];
// Retrive text
UIPasteboard* board = [UIPasteboard pasteboardWithName:#"com.company.wtv" create:YES];
board.persistent=YES;
NSData* result=nil;
NSString*resultStr=nil;
result =[board valueForPasteboardType:#"com.company.wtv.sharedValue"];
resultStr=[[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];// I got resultStr containing
123456ccc
NSLog(#"key %#",resultStr);

Pass paste content to a NSString

I'm to make a button that pastes the "paste" content into a UITextView, when the user clicks a UIButton.
How could I do that?
And, can I send the value to a NSString before? Then verify if it's a valid link?
Very easy:
UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
textView.text = pasteBoard.string;

Paste from unique PasteBoard (app-specific pasteboard)

Im Saving to a unique pasteboard here:
UIPasteboard *pasteboard = [UIPasteboard pasteboardWithName:#"myPasteboard" create:YES];
[pasteboard setPersistent:YES];
//save to unique pasteboard
[pasteboard setString: [NSString stringWithFormat:#"%#",self.myTextFieldHere]];
Trying to read it out here:
UIPasteboard *pasteSaved = [UIPasteboard pasteboardWithName:#"myPasteboard"];
_myTextFieldHere.text = [pasteSaved string];
My error is "no class method for selector" for my local variable of pastesaved
What ive tried so far
UIPasteboard *pasteSaved =[[UIPasteboard pasteboardTypes] containsObject:#"myPasteBoard"];
UIPasteboard *pasteSaved = [UIPasteboard pasteboardWithName:#"myPasteboard"];
UIPasteboard *pasteSaved = [UIPasteboard pasteboardWithUniqueName:#"myPasteboard"];
UIPasteboard *pasteSaved = [UIPasteboard: #"myPasteboard"];
UIPasteboard *pasteSaved = [UIPasteboard pasteboardWithUniqueName];
Fixed it
It appears than when using a app specific pasteboard you need to add if your creating a pasteboard or receiving from it using create YES or No
Copy to Pasteboard
UIPasteboard *pasteboard = [UIPasteboard pasteboardWithName:#"myPasteboard" create:YES];
[pasteboard setPersistent:YES];
//save to unique pasteboard
[pasteboard setString: [NSString stringWithFormat:#"%#",self.myTextFieldHere]];
Paste from Pasteboard
UIPasteboard *pasteboard = [UIPasteboard pasteboardWithName:#"ICPatEditionPasteboard" create:NO];
self.myTextFieldHere.text = [pasteboard string];
After you have created your unique pasteboard, you paste items to it using the addItems: method:
[pasteboard addItems:#[ #"my_string_for_pasting" ]];
Alternatively,
[[UIPasteboard pasteboardWithUniqueName:#"myPasteboard"] addItems:#[ #"my_string_for_pasting"];
EDIT:
To read from the pasteboard:
NSString *copiedString = [[UIPasteboard pasteboardWithUniqueName:#"myPasteboard"] valueForPasteboardType:kUTTypePlainText];

Pasteboard - copying (image + text) creates mystery \n line break

When I add an image and a string to the pasteboard I get a mysterious line break at the start of my text?
NSMutableDictionary *photo = [NSMutableDictionary dictionary];
NSMutableDictionary *text = [NSMutableDictionary dictionary];
NSData* imageData = UIImagePNGRepresentation(theImage.image);
[photo setValue:imageData forKey:(NSString*)kUTTypePNG];
[text setValue:theText.text forKey:(NSString *)kUTTypeUTF8PlainText];
[[UIPasteboard generalPasteboard] setItems:[NSArray arrayWithObjects:photo, text, nil]];
It puts the photo at the top and then it puts a line break and then the text. If I try to send the image and text in an iMessage the image separates anyways and I end up with the text in a bubble with a silly looking line-break infront of it.
If I just copy the text without the photo it does not add a mysterious line-break.
[[UIPasteboard generalPasteboard] setItems:[NSArray arrayWithObjects:text, nil]];
Does anyone know how to fix this?
If not, can anyone think of some nice workaround? Are there any unicode characters that will reverse a line-break? etc?
Store PNG as BASE64 encode. See if this helps.

iOS Copy and Paste

I'm creating an app to save my copied items anytime I copy something on my iOS device.
Is there anyway I can create an event so that anytime I copy something from any app on my iOS device it saves it into my app?
I want it to fire anytime I copy text so that it pastes it to my apps textbox.
- (void)copy {
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = #"String";
}
- (void)paste {
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSString *string = pasteboard.string;
NSLog(#"%#",string");
}
Refer this Link UIPasteBoard
Take a look at the UIResponderStandardEditActions informal protocol:
https://developer.apple.com/library/ios/documentation/uikit/reference/UIResponderStandardEditActions_Protocol/UIResponderStandardEditActions.html
The key is to ensure that your view controller can become first responder and then implement the following methods:
- (void)copy:(id)sender;
- (void)paste:(id)sender;
UIPasteboard *pb = [UIPasteboard generalPasteboard];
NSDictionary *CellData = [NSDictionary dictionaryWithDictionary:[ArrayName objectAtIndex:SelectedIndexPath.row]];
NSString* strText = [(NSDictionary*)[(NSString*)[CellData objectForKey:#"Key"] JSONValue] objectForKey:#"english"];
[pb setString:strText];

Resources