Skip to content

Instantly share code, notes, and snippets.

@conceptdev
Created May 14, 2012 05:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conceptdev/2691872 to your computer and use it in GitHub Desktop.
Save conceptdev/2691872 to your computer and use it in GitHub Desktop.
MonoTouch helper class for UIPasteboard
using System;
using MonoTouch.UIKit;
using MonoTouch.Foundation;
namespace Clipboard {
public static class ClipboardHelper {
/// <summary>
/// Inserts HTML to clipboard, so you can paste into Mail.app
/// </summary>
/// <remarks>
/// Ported from http://mcmurrym.wordpress.com/2010/08/13/pasting-simplehtml-into-the-mail-app-ios/ by
/// Bill Holmes billholmes54@gmail.com on monotouch@lists.ximian.com
/// Plus stackoverflow.com questions:
/// http://stackoverflow.com/questions/6566152/ios-how-to-copy-html-into-the-cut-paste-buffer
/// http://stackoverflow.com/questions/1034509/copy-and-paste-on-iphone-with-multiple-data-representations/1078471#1078471
/// </remarks>
/// <param name='html'>
/// Pass in the HTML fragment to copy to clipboard
/// </param>
/// <param name='text'>
/// Text version of the data to be put on the clipboard (for notepad and plain UITextViews)
/// </param>
public static void AddToClipboard (string html, string text)
{
Console.WriteLine (@"Place HTML on the pasteboard");
UIPasteboard pasteboard = UIPasteboard.General;
// Uniform Type Identifiers Reference
// https://developer.apple.com/library/ios/#documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html
string htmlType = @"Apple Web Archive pasteboard type";
string textType = @"public.utf8-plain-text";
// HTML payload
NSString htmlString = new NSString (html);
NSMutableDictionary resourceDictionary = new NSMutableDictionary ();
resourceDictionary.Add (new NSString ("WebResourceData"), htmlString.Encode (NSStringEncoding.UTF8));
resourceDictionary.Add (new NSString ("WebResourceFrameName"), new NSString (""));
resourceDictionary.Add (new NSString ("WebResourceMIMEType"), new NSString ("text/html"));
resourceDictionary.Add (new NSString ("WebResourceTextEncodingName"), new NSString ("UTF-8"));
resourceDictionary.Add (new NSString ("WebResourceURL"), new NSString ("about:blank"));
NSDictionary containerDictionary = NSDictionary.FromObjectsAndKeys (
new object [] {resourceDictionary},
new object [] {new NSString ("WebMainResource")});
NSDictionary htmlItem = NSDictionary.FromObjectsAndKeys (
new object [] {containerDictionary},
new object [] {new NSString(htmlType)});
// TEXT payload
NSDictionary textItem = NSDictionary.FromObjectsAndKeys (
new object [] {new NSString(text)},
new object [] {new NSString(textType)});
// finally, add to PASTEBOARD
NSDictionary[] items = new NSDictionary [] {htmlItem, textItem};
if (pasteboard.Contains (new string[] {htmlType, textType} )) {
pasteboard.Items = items;
Console.WriteLine ("Replace existing items");
} else {
pasteboard.AddItems (items);
Console.WriteLine ("Added items");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment