How to do copy/paste function programmatically in iphone?

ICoder picture ICoder · Nov 8, 2011 · Viewed 26.7k times · Source

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.

Answer

Shishir.bobby picture Shishir.bobby · Nov 8, 2011

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