Pass paste content to a NSString - ios

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;

Related

Copying HTML using UIPasteBoard and pasting into Gmail

I am trying to find a way to use UIPasteBoard to copy a HTML string into pasteboard, and be able to paste it into different mail clients on iOS.
I tried two accepted answers from StackOverflow: https://stackoverflow.com/a/6566850/1249958 and https://stackoverflow.com/a/21911997/1249958. Those solutions are working on default iOS mail client but they don't make any difference on Gmail.
I know that Gmail accepts HTML input from pasteboard as copying HTML from Safari and pasting that into Gmail app works as expected.
this will put HTML and plain text on the pasteboard and can be pasted into GMail.
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSString *iOSRichContentKey = #"iOS rich content paste pasteboard type";
NSData *iOSRichContent = [iOSRichContentKey dataUsingEncoding:NSUTF8StringEncoding];
NSString *sampleHTML = #"This is <span style='font-weight:bold'>HTML</span>";
NSString *appleWebArchiveKey = #"Apple Web Archive pasteboard type";
NSData *sampleHTMLData = [sampleHTML dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dict = #{#"WebMainResource": #{#"WebResourceData": sampleHTMLData, #"WebResourceFrameName": #"", #"WebResourceMIMEType": #"text/html", #"WebResourceTextEncodingName": #"UTF-8", #"WebResourceURL": #"about:blank"}};
NSData *appleWebArchive = [NSPropertyListSerialization dataWithPropertyList:dict format:NSPropertyListBinaryFormat_v1_0 options:0 error:nil];
NSString *plainTextKey = #"public.utf8-plain-text";
NSString *plainText = #"this is plain text";
[pasteboard setItems:#[#{iOSRichContentKey : iOSRichContent, appleWebArchiveKey : appleWebArchive, plainTextKey : plainText}]];

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);

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];

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];

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

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;

Resources