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];
Related
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);
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;
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];
I have the following test, failing
NSString * expectedValue = #"achilles";
UIPasteboard *pasteboard = [UIPasteboard pasteboardWithName:#"pb1" create:YES];
pasteboard.persistent = YES;
pasteboard.string = expectedValue;
STAssertEqualObjects(expectedValue, [pasteboard string], #"get written value from pasteboard");
[pasteboard setString:expectedValue];
STAssertEqualObjects(expectedValue, [pasteboard string], #"get written value from pasteboard");
Both of the asserts fail,
'achilles' should be equal to '(null)'
Am I incorrectly writing to the pasteboard, reading from the pasteboard, or both?
I assume the problem is the following:
The string property of UIPasteboard is defined as (see the docs)
#property(nonatomic, copy) NSString *string;
This means that if you assign a string to it, it will be copied, i.e. a new object is created. When you read it back and compare it to the original object, the compare must fail.
If you are writing or reading a string from UIPasteBoard you can easily do it by accessing,
[UIPasteboard generalPasteboard].string = #"your string";
NSString *str = [UIPasteboard generalPasteboard].string];
For reading and writing NSString you can use general paste board.
there are two simple ways to write...
//Method 1
NSString * str=#"your String";
UIPasteboard * pasteboard=[UIPasteboard generalPasteboard];
[pasteboard setString:];
//Method 2
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
[pasteboard setData:data forPasteboardType:(NSString *)kUTTypeText];
and you can read NSString as bellow
UIPasteboard * pasteboard=[UIPasteboard generalPasteboard];
//Method 1
NSLog(#"Text =%#",[pasteboard string]);
//Method 2
NSData * data = [pasteboard dataForPasteboardType:(NSString*)kUTTypeText];
NSString * str =[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"str =%#",str);
For more info you can refer this Blog
Your code passed the testing in Unit Testing. You should point out your development environment if you did have trouble in it.
Test Case '-[TestTests testExample]' started.
Test Case '-[TestTests testExample]' passed (0.001 seconds).
Tested in Xcode 5.1.1 with SDK iOS7.1
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;