Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do copy/paste function programmatically in iphone?

I have a text-view with some text and a copy button in that view,

When the user enters some text and presses the copy button, it needs to copy that text and paste that text wherever he wants.

I know there is a default copy/paste menu-controller in iOS, but I want to do this functionality in a button click. I think there is UIPasteboard to do this functionality, but I don't know how to use it.

like image 337
ICoder Avatar asked Nov 08 '11 04:11

ICoder


People also ask

How do you paste on iPhone without tapping?

To paste the copied content onto the screen, perform a pinch-out or pinch-open gesture on the screen using three of your fingers (most preferably, thumb + index + middle fingers or index + middle + ring fingers).


2 Answers

To copy from a button click:

- (IBAction)copy {     UIPasteboard *pb = [UIPasteboard generalPasteboard];     [pb setString:[textView text]]; } 

To paste from a button click:

- (IBAction)paste {     UIPasteboard *pb = [UIPasteboard generalPasteboard];     textView.text = [pb string]; } 
like image 138
Shishir.bobby Avatar answered Oct 08 '22 00:10

Shishir.bobby


Swift

This is the Swift version of the accepted answer.

Copy

UIPasteboard.general.string = myTextView.text 

Paste

if let myString = UIPasteboard.general.string {     myTextView.insertText(myString) } 
like image 45
Suragch Avatar answered Oct 07 '22 23:10

Suragch