UIActivityViewController with HTML

2019-03-11 06:44发布

问题:

When I pass a string containing HTML content as an array element to the UIActivityViewController initWithActivityItems method it doesn't present it as HTML in the selected 'share' controller, but instead presents the HTML source (i.e. the markup rather than interpreting it).

And while I can understand why it would do this for share targets that don't support html content, it won't interpret it for things like email either, which do (previously this would be done by setting the isHTML property to YES on the MFMailComposeController).

Should I waste my time creating a UIActivityItemProvider that returns custom content based on the 'activity type' or is there really no way to get the mail controller presented by the UIActivityViewController to interpret the content as HTML?

回答1:

What activities are you looking to support? Activity types are dependent on the service, and they're pretty clearly defined. There's no way for services to know that a string is a plain text or marked-up text, for example--you might want to paste HTML code on purpose.

You can always do things the old-fashioned way and copy plain text and HTML to the pasteboard, or call the mail composer directly, or whatever you're trying to accomplish.



回答2:

In my testing if the string begins "<html><body>" and ends "</body></html>" then it is treated as HTML.

If you want a good result with the non-HTML-aware sharing services you need to instead use an object that implements the UIActivityItemSource protocol and returns the HTML string when from -activityViewControllerPlaceholderItem: and from -activityViewController:itemForActivityType: if the activity is UIActivityTypeMail and nil otherwise.

A second UIActivityItemSource that returns a suitable non-HTML string from -activityViewControllerPlaceholderItem: and from -activityViewController:itemForActivityType: if the activity is not UIActivityTypeMail (and nil if it is) is the rest of the puzzle.

I recommend against having one object do both jobs, as the UIActivity engine is entitled to make different decisions based on whether the placeholder item appears to be HTML or not.



回答3:

I haven't tried any of these, but they seemed reasonable for your goal when I was getting myself familiar with UIActivityViewController.

1) You could write the HTML source to a local file, e.g., my_mail_message.html, then build an NSURL object with that file, e.g., [NSURL URLWithString:@"file://my_mail_message.html"], and then use that NSURL object as an element of the activityItems array that you feed to initWithActivityItems:applicationActivities:.

2) If you know the intended recipient, e.g., foo@example.com, you could possibly build an NSURL object with the mailto scheme and the HTML source as the body, like so: mailto:foo@example.com?body={my HTML source goes here}. You should omit the curly braces {}. As before, you should then use that NSURL object as an element of the activityItems array.

Hope this gives you some ideas.



回答4:

You just need to make sure the first line of the HTML string is an tag.