Search code examples
iosiphonecopy-paste

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.


Solution

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