Skip to content


Monotouch Quickie : UIPasteboard Snippet

Came across the need to plop a string onto the clipboard (aka UIPasteboard) programatically last night and hit up the MonoTouch docs to see what was involved.  It wasn’t readily apparent what would work for the string expected to be passed as the SetValue method pasteboardType argument.  Turns out, after digging into the official Apple docs, the string you need to provide is the Uniform Type Identifier (UTI) that corresponds to the data type being passed (in my case “public.utf8-plain-text”).  For my future reference (always seems to take longer to find things at the iPhone doc site) here is a table of the relevant UTIs that might come in handy in the future.

string public.utf8-plain-text
JPEG public.jpeg
PNG public.png
Url public.url

 

And for good measure here is a snippet that shows putting a string on the general clipboard and reading it off.

string clipboardText = "Copy and Paste Me!";

UIPasteboard.General.SetValue(clipboardText,
"public.utf8-plain-text");
string actualClipboardText = UIPasteboard.General.GetValue("public.utf8-plain-text");

Assert.AreSame( clipboardText, actualClipboardText.ToString());

Enjoy.

[Updated(1/30/2010): Replaced NSStrings out with strings]

Posted in UIPasteboard, monotouch. Tagged with , , , , , , , , , , .

4 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Miguel de Icaza said

    Most of the time there is no need to use the NSString types in your code, the runtime will do those conversions for you on demand (string->NSString).

    The cases where you might want to create those in advance are the times where the same string will be used repeatedly (for example when strings are used as keys in a collection) and you want to avoid creating unnecessary objects.

  2. Thanks Miguel. I’ll update the example.

Continuing the Discussion

  1. MonoTouch.Info linked to this post on January 26, 2010

    MonoTouch Article – Monotouch Quickie – UIPasteboard Snippet…

    Thank you for submitting this entry – Trackback from MonoTouch.Info…

  2. Tweets that mention Monotouch Quickie : UIPasteboard Snippet | KevFoo -- Topsy.com linked to this post on January 26, 2010

    [...] This post was mentioned on Twitter by Kevin McMahon, MonoTouch.Info. MonoTouch.Info said: RT @klmcmahon: New blog post: Monotouch Quickie : UIPasteboard Snippet http://blog.kevfoo.com/index.php/2010/01/monotouch-quickie-uipasteboard-snippet/ [...]

Some HTML is OK

(required)

(required, but never shared)

or, reply to this post via trackback.