Tutorial: Introducing the Pasteboard plugin

Share on Facebook0Share on Google+0Tweet about this on TwitterShare on LinkedIn0

A common requirement for business app developers is the ability to copy and paste items — more specifically, the ability to select something from your app and paste it into another, or copy something from another app and paste it into yours.

Corona’s native text objects native.newTextField() and native.newTextBox() already support copying and pasting text using the operating system’s methods, but because normal text displayed via display.newText() is rendered into a graphical object, you couldn’t select those characters and copy them elsewhere, until now.

Pasteboard plugin

To get started, simply include the plugin in your build.settings file:

Next, in main.lua or other modules where you want to use this feature, require the plugin as follows:

Selecting and copying

The Pasteboard plugin can be used to bring in data from other apps, or provide data to other apps. Using it, you have the option of selecting:

  • an image
  • a string
  • a URL

Selecting objects in Corona-based apps is not done in the same way as a web browser or text editor, since it’s primarily rendered graphics/objects. You cannot simply touch an area of the screen, drag “select handles” around something, and gather the contents. Instead, you must program some method which allows the object to be selected, like a button or a touch handler upon the object. You could also change the object visually, for example, invert its color using the invert filter. In the following simple example, if the user touches the text object, you invert the color and copy the value of that text object (its string) to the pasteboard using the pasteboard.copy() API:

To copy an image, use the copy function with the copyType parameter set to "image":

For a URL, follow the "string" model, but specify "url" instead:

You could even present a series of images, each with an associated URL, and when the user touches the image, it copies a URL instead. The potential options are considerable!

Pasting content into your app

Getting data from other apps is useful as well. As noted above, native objects like native.newTextField() and native.newTextBox() already support pasting of text copied from other sources. To paste content into other aspects of your app, you’ll need a paste handler function and a way to invoke it. Consider this code:

In this block of code, we set up two objects to handle the pasted-in values: an image called pastedImage and a text object called textField. Then, based on the type of content being pasted in, the object is filled/updated accordingly.

Other routines

You can limit the types of items being pasted in by calling:

If you only want to allow strings, include just the "string" entry in the array.

You can also determine the type of data currently on the pasteboard:

Finally, you can empty the pasteboard by calling the clear() API:

Share on Facebook0Share on Google+0Tweet about this on TwitterShare on LinkedIn0
Rob Miracle

Rob Miracle creates mobile apps for his own enjoyment and the amusement of others. He serves the Corona Community in the forums, on the blog, and at local events.

This entry has 5 replies

  1. Skeyta says:

    Awesome! Finally, been waiting for this for a loong time. Great work corona team!

  2. Greg says:

    would be very useful to be able to test on the simulator – will this be the case in the 1.0 version? or is there some constraint with the plugin’s approach that prevents this?

    • Rob Miracle says:

      There probably isn’t an OS-X equivalent to the iOS SDK we are using. Obviously OS-X supports the concepts of copy/paste, but the underling architecture for it could be drastically different.


  3. vikas says:

    Is this plugin is free for all users or only pro/enterprise account holders can use this. Thanks!

    • Rob Miracle says:

      It’s free for all users.