开发者

copy HTML to UIPasteboard

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 pastebo开发者_如何学Pythonard 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


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/


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>


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜