8

I am trying to find a way to use UIPasteBoard to copy a HTML string into pasteboard, and be able to paste it into different mail clients on iOS.

I tried two accepted answers from StackOverflow: https://stackoverflow.com/a/6566850/1249958 and https://stackoverflow.com/a/21911997/1249958. Those solutions are working on default iOS mail client but they don't make any difference on Gmail.

I know that Gmail accepts HTML input from pasteboard as copying HTML from Safari and pasting that into Gmail app works as expected.

halilb
  • 4,055
  • 1
  • 23
  • 31

1 Answers1

0

this will put HTML and plain text on the pasteboard and can be pasted into GMail.

UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];

NSString *iOSRichContentKey = @"iOS rich content paste pasteboard type";
NSData *iOSRichContent = [iOSRichContentKey dataUsingEncoding:NSUTF8StringEncoding];

NSString *sampleHTML = @"This is <span style='font-weight:bold'>HTML</span>";

NSString *appleWebArchiveKey = @"Apple Web Archive pasteboard type";
NSData *sampleHTMLData = [sampleHTML dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dict = @{@"WebMainResource": @{@"WebResourceData": sampleHTMLData, @"WebResourceFrameName": @"", @"WebResourceMIMEType": @"text/html", @"WebResourceTextEncodingName": @"UTF-8", @"WebResourceURL": @"about:blank"}};
NSData *appleWebArchive = [NSPropertyListSerialization dataWithPropertyList:dict format:NSPropertyListBinaryFormat_v1_0 options:0 error:nil];

NSString *plainTextKey = @"public.utf8-plain-text";
NSString *plainText = @"this is plain text";

[pasteboard setItems:@[@{iOSRichContentKey : iOSRichContent, appleWebArchiveKey : appleWebArchive, plainTextKey : plainText}]];
matt bezark
  • 236
  • 3
  • 5