IOS: How to copy HTML to cut buffer?

I am interested in allowing my users to copy the text that they entered into the cut and paste buffer, but I would like to do this as HTML.

Is this possible? Or do I need to use the MIME format? (I have no idea.)

Thanks.

+6
ios clipboard cocoa-touch uikit uipasteboard
source share
3 answers

The following code will output your HTML from your application and into the Apple Mail application. The documentation doesn't give you much help with this, so part of the question is which Apple apps park on cardboard and then remodel it. This solution builds on an earlier https://stackoverflow.com/a/16677664/ ... - follow the links where there is more background.

NSLog(@"Place HTML on the pasteboard"); UIPasteboard* pasteboard = [UIPasteboard generalPasteboard]; NSString *htmlType = @"Apple Web Archive pasteboard type"; // example html string NSString* htmlString = @"<p style=\"color:gray\"> <a href=@\"http://itunes.apple.com/gb/app/paragraft/id412998778?mt=8\">Paragraft</a><br><em>Less than a word processor, more than plain text</em>"; NSMutableDictionary *resourceDictionary = [NSMutableDictionary dictionary]; [resourceDictionary setObject:[htmlString dataUsingEncoding:NSUTF8StringEncoding] forKey:@"WebResourceData"]; [resourceDictionary setObject:@"" forKey:@"WebResourceFrameName"]; [resourceDictionary setObject:@"text/html" forKey:@"WebResourceMIMEType"]; [resourceDictionary setObject:@"UTF-8" forKey:@"WebResourceTextEncodingName"]; [resourceDictionary setObject:@"about:blank" forKey:@"WebResourceURL"]; NSDictionary *containerDictionary = [NSDictionary dictionaryWithObjectsAndKeys:resourceDictionary, @"WebMainResource", nil]; NSDictionary *htmlItem = [NSDictionary dictionaryWithObjectsAndKeys:containerDictionary,htmlType,nil]; [pasteboard setItems: [NSArray arrayWithObjects: htmlItem, nil]]; // This approach draws on the blog post and comments at: // http://mcmurrym.wordpress.com/2010/08/13/pasting-simplehtml-into-the-mail-app-ios/ 
+6
source share

This solution puts both HTML and a simple textual representation in a cardboard:

 #import <MobileCoreServices/MobileCoreServices.h> NSString *html = @"<h1>Headline</h1>text"; NSData *data = [html dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *dict = @{@"WebMainResource": @{@"WebResourceData": data, @"WebResourceFrameName": @"", @"WebResourceMIMEType": @"text/html", @"WebResourceTextEncodingName": @"UTF-8", @"WebResourceURL": @"about:blank"}}; data = [NSPropertyListSerialization dataWithPropertyList:dict format:NSPropertyListXMLFormat_v1_0 options:0 error:nil]; NSString *archive = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSString *plain = [html stringByReplacingOccurrencesOfRegex:@"<[^>]+>" withString:@""]; [UIPasteboard generalPasteboard].items = @[@{@"Apple Web Archive pasteboard type": archive, (id)kUTTypeUTF8PlainText: plain}]; 

It uses -stringByReplacingOccurrencesOfRegex: from RegexKitLite to remove HTML tags.

+4
source share

I absolutely adore this method of creating HTML content that can be pasted into other HTML-enabled applications, such as Mail. However, I noticed that the aforementioned Matthew Elton solution allowed the insertion of a file cabinet into applications that support HTML. For example, an attempt to insert the same content into a Notes application will fail.

I took the advice from this post: https://stackoverflow.com/a/312960/ and can now successfully insert both HTML and text versions of the content I want.

0
source share

All Articles