Join Steven Lipton for an in-depth discussion in this video Copy and paste with the pasteboard, part of iOS Development Tips Weekly.
- All day long, you use Command X, Command C, and Command V to cut, copy and paste from the pasteboard. You see that functionality in other applications. How do you get that functionality? In this video, I'll show you the basics of using the pasteboard to copy and paste data, not only in your app, but over the sandbox wall and into other applications. If you run the starter file, I've set up a small application to type into the text view. Go ahead and run it. And you'll see that there are two buttons at the top for copying and pasting the text.
Stop the app and we'll make the code for these buttons. Head into View Controller and before we do anything else, I'm going to add a constant. Let pasteboard equal UIPasteboard dot general. This is the general pasteboard. It's the only persistent pasteboard, and the only pasteboard which can copy and paste outside your app. Move down the code a bit to the copy text action.
Many of the simple types, like strings and numbers, have special methods and properties to quickly copy to the pasteboard. So the simplest way to copy text is this. Pasteboard, dot string, equals, typing text, which is my text view, and I'm just going to get the text property of that. Like so. I'll actually make this more of a cut than a copy and so I'm going to delete the text after I do this.
So I'm going to type in text, dot text, and I'll make it a blank string. Taking the text off the pasteboard and pasting it is just as easy. In the paste text action, assign the pasteboard text to the text view. Type in text, dot text, equals pasteboard dot string. Go ahead and run this on the simulator. I'm going to type on my keyboard.
I love pizza. And then I'll copy it. I'll then just paste it back, and it appears. The general pasteboard has one ability other pasteboards don't. You can copy and paste between apps. This behavior does not work in the simulator though, only a real device. I'll stop the app in the simulator. I've plugged in my iPad mini, and I'll run it there.
This time, I'll jump out of the app and into Notes, where I have something already written. I'll select and copy it. And then go back to mini typer, and hit paste. I'll use copy to clear the screen, and then type something out. Copy it again.
Slip back to Notes. And on the bottom of my note here, I paste it. Okay, go ahead and stop the app. Now you can also cut and paste images using the image property. Of course, you'll have to know what is an image and what is a string. To do that, we'll check if the pasteboard is a string or an image. I'll add this as the first line of the paste test action. So make a line of typing text there and put if pasteboard dot has strings, put the opening bracket and then the closing bracket behind the typing text.
The has strings quickly checks the pasteboard for strings. If there, it'll paste a string. Under that, I'll add another clause to do the same thing for images. If pasteboard dot has images, outlet background image, which is a ui image view on this, so I'll put it into there, and I'll put the image equal to the pasteboard image.
So this code checks for an image on the pasteboard, and then, if in an image, pasted the background image view I set up in the app. Go ahead and run this. And we're back in our app. I'll slide over to photos where I have some food photography handy. And I'll pick one of these. Let's say French toast here. And I'm going to copy it.
Slide back to my app, and hit paste. And my photo appears. That's the basic with some of the simplest ways of copying and pasting using the clipboard. There's more to explore here, including other objects such as color and data. Including multiple objects on the pasteboard and your own internal pasteboards. Click the Apple documentation for UI Pasteboard for more details.