4

I want to transfer HTML from my app to the iPhone mail application. I already have the HTML text - lest say <span style='color:red'>Test</span> I can place this to UIPasteBoard - but when I paste it to mail I get the html source.

When I place the same string in a HTMLView - select it there and copy it it pastes as red text in mail.

What do I have to do to place the string in the UIPasteBoard so that it pastes as red text to the mail application? I've been searching for "format types" - and found that UIPasteBoard returns "Aplle Web Archive pasteboard type" when I have the element (copied from UIWebView) in the clipboard. But setting this as type when adding the content to UIPasteBoard pastes nothing in the mail app.

Manfred

F'x
  • 12,105
  • 7
  • 71
  • 123
ManniAT
  • 1,989
  • 2
  • 19
  • 25

5 Answers5

9

That is not true. you can paste ANYTHING to the pasteboard, go read the docs.

I've finally put together a tutorial that shows how to copy HTML into the Mail app. http://mcmurrym.wordpress.com/2010/08/13/pasting-simplehtml-into-the-mail-app-ios/

maxpower
  • 1,203
  • 11
  • 20
  • Could you please tell me, where in the 3.x UIPasteBoard DOCs these other ("ANYTHING") could be found? I followed your friendly suggestion and read the docs: http://developer.apple.com/iphone/library/documentation/uikit/reference/UIPasteboard_Class/Reference.html#//apple_ref/doc/uid/TP40008243-CH1-SW49 Maybe there are other docs I should read - just tell me where they are. – ManniAT Aug 02 '10 at 09:36
6

I've got HTML copy working so it pastes into the built-in Mail and Notes apps properly. It looks like this:

NSString *htmlContent = @"This is <span style='font-weight:bold'>HTML</span>";
NSString *content = @"This is HTML!";
NSDictionary *dict = @{(NSString *)kUTTypeText: content, (NSString *)kUTTypeHTML: htmlContent};
[[UIPasteboard generalPasteboard] setItems:@[dict]];

To get access to those type constants, you need to import this:

#import <MobileCoreServices/UTCoreTypes.h>
Tom Hamming
  • 10,577
  • 11
  • 71
  • 145
1

on that same link you gave in your comment, you'll find this paragraph at the top.

A Uniform Type Identifier (UTI) is frequently used for a representation type (sometimes called a pasteboard type). For example, you could use kUTTypeJPEG (a constant for public.jpeg) as a representation type for JPEG data. However, applications are free to use any string they want for a representation type; however, for application-specific data types, it is recommended that you use reverse-DNS notation to ensure the uniqueness of the type (for example, com.myCompany.myApp.myType).

Right below it is a link to here. http://developer.apple.com/iphone/library/documentation/FileManagement/Conceptual/understanding_utis/understand_utis_intro/understand_utis_intro.html#//apple_ref/doc/uid/TP40001319

Which explains UTIs.

Finally this link gives you SEVERAL types http://developer.apple.com/iphone/library/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html#//apple_ref/doc/uid/TP40009259-SW1

Of course that list isn't ALL types as you can create your own types.

I have successfully pasted html into the mail app. I'll give you a good place to start...

Create an app that will show the data types in the pasteboard. Goto safari on the device, copy a web page. Run your app, You'll notice the pasteboard type is "Apple Web Archive pasteboard type." Note that this is really a pasteboard type (a custom one). If you try to duplicate the safari mobile copy and paste feature yourself by creating a web archive and attempt to paste it as text into the mail app, it will show the web archive file as raw xml. If you define the type as "Apple Web Archive pasteboard type" the mail app will actually format the paste as html.

If you want to know what a web archive looks like. On desktop safari, just save a web page as an archive and look at the file in a text reader (text edit will try to parse it, so you might use a different program to look at the archive xml).

Please read all of the documentation as you can discover that you can do custom types in the link that you sent me.

maxpower
  • 1,203
  • 11
  • 20
  • The problem is (on iPhone not desktop), - as I wrote in the initial question - that when I set the type to (for an example) "Apple Web Archive pasteboard type" nothing pastes in the email. I also found the extension for UTIs - but I couldn't get it work nor did I find an example on the web. All I found (which was also the first answer here) was that "on iPhone only ... are available". – ManniAT Aug 03 '10 at 09:50
  • Ah, and by the way (sry that I forgot) thank you for your effort. If you could provide provide a link to some working sample this would be great. – ManniAT Aug 03 '10 at 09:52
  • My solution is for the iphone/ipad. I'll see if I can get around to creating an example. The problem you are running into is that the string associated with the type for Apple Web Arc.... is that it can't be just a plain string. It is a rather involved process. You need to create an XML document/string and the actual html contents within are base64 encoded. Feel free to at least take the negative vote off :) I should have given you a detailed answer initially, sorry for being short. – maxpower Aug 03 '10 at 19:20
  • I'm also working on pasting correctly into numbers for ipad which I'll see if I can get an example up for that as well, but I've just started that path so it might take a little bit of time, not sure. – maxpower Aug 03 '10 at 19:21
0

It is absolutely possible. UIPasteboard usually contains multiple values, indexed by their UTI.

Here is a working example of how to get public.html as a primary type.

-(NSString * _Nullable) getHtmlFromPB {
    static NSArray * prefs = @[@"public.html",@"public.utf8-plain-text",@"public.plain-text"];
    if (!UIPasteboard.generalPasteboard.hasStrings) return nil;
    for (NSString * theone in prefs) {
        if ([[UIPasteboard generalPasteboard].pasteboardTypes indexOfObject:theone] == NSNotFound) continue;
        return [UIPasteboard.generalPasteboard.items.firstObject objectForKey:theone];
    }
    return nil;
}

If you need explaination: prefs array contains preferred UTI for Pasteboard content in desired order. So, it will take one of them or nothing.

ETech
  • 1,613
  • 16
  • 17
  • Sorry but this question was asked more than 10 years ago - things have changed a lot. – ManniAT Jun 13 '21 at 19:28
  • Anyway, the question still actual. I've seen it long time ago and had to understand and implement `UIPasteboard` concepts and details, based on @F'x NO. Things have changed, but this No is still The best answer and number of people (me too 5 years ago) can consider - it's true and just take first element from `UIPasteboard` that probabluy will be difficult to process Apple's rtf. So, changes have to lead to updates to know about them. – ETech Jun 14 '21 at 08:06
-17

No, it cannot. UIPasterBoard only accepts strings, images, URLs and colors.

F'x
  • 12,105
  • 7
  • 71
  • 123
  • I was afraid someone would says this :) Thank you – ManniAT Mar 16 '10 at 10:05
  • 2
    Although this answer is marked as correct, as noted below, you can copy HTML to the UIPasteBoard. See maxpower's answer below. For a short snippet of code to paste HTML from your app into the iOS mail app see http://stackoverflow.com/q/6566152/531205 – Obliquely Jul 04 '11 at 02:44
  • Totally incorrect! Even the UIPasteBoard reference states that you can put your custom file types on the paste board. – Akshay Sep 29 '11 at 10:32
  • Definitely not true, check the link Matthew posted. I did some red text with that very method just a few minutes ago as a test for something else. It gets pasted into the mail as HTML. – Peter Johnson Dec 20 '12 at 03:07