To get started, download the source code via SVN:

svn checkout http://openclip.org/svn/openclip

This should create a directory called "openclip" that holds a few test projects.

The main class file, OCPasteboard is in the Copy project. You can compile and run the Copy project for the simulator or for the native device. When you do that, you can test that everything is working by typing a few letters in the text box and hitting the copy button. Or, alternatively, you can copy the image.

When you have done that, compile and run the Paste project. The Paste project uses the OCPasteboard files out of the Copy projects directory. When running that, you should be able to paste whatever you copied. If you copied the image, you should be able to paste the image. If you copied the text, you should be able to paste the text.

Copying

Lets look at what I am doing in the Copy project when the user hits the copy button next to the textbox:

OCPasteboard *general = [OCPasteboard generalPasteboard];
[general declareTypes:[NSArray arrayWithObjects:OCStringPboardType, nil] owner:self];
BOOL success = [general setData:[[textView text] dataUsingEncoding:NSUTF8StringEncoding] forType:OCStringPboardType];

The first line grabs the general pasteboard. This is the pasteboard everyone expects data to be on. If you have reasons to privately pass data between applications (for an explanation as to what "privately" means, visit our PrivacyConcerns page), you can create your own clipboard with a unique name or with a name you would like.

The second line declares types that we will be providing. Here we just want to provide a plain text type. But we could alternatively provide a plain text type and an html type.

The third line sets the data on the pasteboard. You can use any encoding you want, but the other side obviously has to know what encoding you are going to use. The success variable checks if the setData worked. If the operation doesn't work, it is important to communicate this to users. This will only fail when the files OpenClip? needs to write can't be written. This honestly shouldn't happen, even after 2.1, so when it does happen, something is really wrong.

Copying an Image

Copying an image is very similar.

OCPasteboard *general = [OCPasteboard generalPasteboard];
[general declareTypes:[NSArray arrayWithObjects:OCPNGPboardType, nil] owner:self];
BOOL success = [general setData:UIImagePNGRepresentation(imageView.image) forType:OCPNGPboardType];

The OCPNGPboardType does not have an NSPasteboard equivalent, but because PNGs and JPEGs are so prevalent on the iPhone, I figured that warranted adding API.

Pasting

Lets look at what I am doing in the Paste project when the user hits the paste button next to the textbox:

OCPasteboard *general = [OCPasteboard generalPasteboard];
NSData *data = [general dataForType:OCStringPboardType];

if (nil != data) {
	textView.text = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
} else {
	//OpenClip could not find any pastes on the clipboard.
}

The first line grabs the general pasteboard.

The second line gets the data for the type we are interested in.

I then check if the data is not nil. If it isn't, we found the latest paste of the requested data type. If data is nil, then we either could not find a paste file, or some error occurred. Either way should probably be treated the same and nothing should happen. For some tips on how to handle these kinds of errors, check out the BestPractices section.

Pasting an Image

OCPasteboard *general = [OCPasteboard generalPasteboard];
NSData *imageData = [general dataForType:OCPNGPboardType];

if (nil != imageData) {
	imageView.image = [UIImage imageWithData:imageData];
} else {
	//OpenClip could not find any pastes on the clipboard.
}

Again, very similar to pasting text, except now we are grabbing the png data if it exists and instantiating a UIImage object from the data acquired.

Addition Help

If you require additional assistance or you aren't sure if OpenClip? is right for you, you can email me at zac...at...openclip.org. I suggest everyone in watching the progress or participating in the development of OpenClip? subscribe to the mailing list here.