EDIT
This is the second attempt. Earlier I understood you wanted to copy and paste the HTML as text but after your comments and edits to the question I now understand that you have some text that you format using HTML and you want to paste that text while retaining the formatting.
More or less as shown below.
This also illustrates how to wire it up in storyboard. Here is the code.
#import "ViewController.h"
#import
#import
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel * statusLabel;
@property (weak, nonatomic) IBOutlet WKWebView * webView;
@property (weak, nonatomic) IBOutlet UITextView * textView;
@end
@implementation ViewController
#pragma mark -
#pragma mark Actions
- (IBAction)reloadButtonAction:(id)sender {
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.webfx.com/blog/images/assets/cdn.sixrevisions.com/0435-01_html5_download_attribute_demo/samp/htmldoc.html"]]];
}
- (IBAction)copyButtonAction:(id)sender {
if ( self.webView.loading ) {
self.statusLabel.text = @"Loading";
} else {
self.statusLabel.text = @"Copying ...";
[self.webView evaluateJavaScript:@"document.documentElement.outerHTML.toString()"
completionHandler: ^ ( id content, NSError * error ) {
if ( error ) {
self.statusLabel.text = @"Error";
} else if ( [content isKindOfClass:NSString.class] ) {
[UIPasteboard.generalPasteboard setData:[( NSString * ) content dataUsingEncoding:NSUTF8StringEncoding]
forPasteboardType:( NSString * ) kUTTypeHTML];
self.statusLabel.text = @"Copied HTML";
} else {
self.statusLabel.text = @"Unknown type";
}
}];
}
}
- (IBAction)pasteButtonAction:(id)sender {
if ( [UIPasteboard.generalPasteboard containsPasteboardTypes:@[( NSString * ) kUTTypeHTML]] ) {
// Convert to attributed string
NSError * cvtErr;
NSData * data = [UIPasteboard.generalPasteboard dataForPasteboardType:( NSString * ) kUTTypeHTML];
NSAttributedString * attr = [[NSAttributedString alloc] initWithData:data
options:@{ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType }
documentAttributes:NULL
error: & cvtErr];
if ( cvtErr ) {
self.statusLabel.text = @"Convert error";
} else if ( attr ) {
self.statusLabel.text = @"Attributed";
self.textView.attributedText = attr;
} else {
self.statusLabel.text = @"Unable to decode";
}
} else {
NSString * content = UIPasteboard.generalPasteboard.string;
if ( content ) {
// Paste plain text
self.textView.text = content;
self.statusLabel.text = @"Pasted";
} else {
self.statusLabel.text = @"No string content";
}
}
}
@end
The code is very similar to before. The problem is that HTML = plain text so to copy HTML and retain the style or formatting also depends on the destination app into which you paste it. That destination app needs to handle the HTML correctly for what you want to achieve.
Anyhow, here are the changes from earlier.
On the copy side: the text is now copied as HTML and not as a string. There is very little difference there except that now, unlike before, the string is converted to data and the type is marked as kUTTypeHTML.
On the paste side: the real difference is here. If the pasteboard contains HTML then it is retrieved and formatted using an attributed string. It also works fine if you e.g. paste into Notes BTW.
This illustrates the problem you are dealing with here. I could just as well have used the code I used earlier and everything would work but in stead of formatted HTML I would get the unformatted / source HTML.
Here I assume that you are interested in fairly simple but formatted text. You did not mention text specifically and if you e.g. wanted to copy an HTML formatted table then my example is not adequate as I use a UITextView as destination.
For more complex HTML I'd use a WKWebView as destination as well and set its HTML string to the pasted in HTML, but again, this shows how the destination app also needs to cooperate in handling the HTML correctly.
Finally, looking at the updated answer and your code it is difficult to spot a difference, so I suspect your problem could be earlier with content itself in
RCT_EXPORT_METHOD(setString:(NSString *)content)
UPDATE
I also looked at the git repository you mention. There I found the following
RCT_EXPORT_METHOD(hasString:(RCTPromiseResolveBlock)resolve
reject:(__unused RCTPromiseRejectBlock)reject)
{
BOOL stringPresent = YES;
UIPasteboard *clipboard = [UIPasteboard generalPasteboard];
if (@available(iOS 10, *)) {
stringPresent = clipboard.hasStrings;
} else {
NSString* stringInPasteboard = clipboard.string;
stringPresent = [stringInPasteboard length] == 0;
}
resolve([NSNumber numberWithBool: stringPresent]);
}
It looks wrong, I think that one line should be
stringPresent = [stringInPasteboard length] != 0;
HTH