Avoiding the movement of an image sending an email using NSSharingService with HTML content

I am trying to send an email using NSSharingService from a Mac application. I am including the HTML as the body of the email. When the Mail window appears, the content will be formatted, but the image in the HTML structure will be moved to the bottom letter. In addition, although I define this image as a link, the link is not executed in the body of the message.

This is the code I'm using:

NSString* htmlText = @"<html><body><p>Message body</p><br/><a href='http://www.google.com'><img border='0' src=http://blog.jimdo.com/wp-content/uploads/2014/01/tree-247122.jpg label='Video' width='512' height='512'></img></a><br/><p>Video: <a href='http://blog.jimdo.com/wp-content/uploads/2014/01/tree-247122.jpg'>http://blog.jimdo.com/wp-content/uploads/2014/01/tree-247122.jpg</a></p><br/><p>Another link to <a href='http://www.google.com' target='_blank'>google</a></p></body></html>"; NSData* textData = [NSData dataWithBytes:[htmlText UTF8String] length:[htmlText lengthOfBytesUsingEncoding:NSUTF8StringEncoding]]; NSAttributedString* textAttributedString = [[NSAttributedString alloc] initWithHTML:textData options:nil documentAttributes:nil]; NSSharingService *emailSharingService = [NSSharingService sharingServiceNamed:NSSharingServiceNameComposeEmail]; [emailSharingService setSubject:@"Subject"]; emailSharingService.delegate = self; NSArray* shareItems = [NSArray arrayWithObjects: textAttributedString, nil]; [emailSharingService performWithItems:shareItems]; 

The image should appear after the text line "Message body" and before "Video: ...", but in my case it is displayed below.

HTML code is tested on Firefox and Safari. Alternatively, I can open the HTML text with Safari and select β€œFile-> Share-> Email This Page”, it does exactly what I'm trying to do (but using Safari, not with my application).

Any idea is friendly. Thanks in advance.

+6
source share
2 answers

I have done many tests using NSSharingService, including concatenating nsattributedstrings and using NSTextAttachment, but it seems that NSSharingService (or NSAttributedString) always pushes the image to the end.

Try using Apple Scripting Bridge.

+2
source

This is a very old question, but it seems that no answer was found because it was asked at the moment (xCode 7 OSX 10.11), the problem is still present.

The solution I found is a bit hacky, but it simplifies and works (with mail.app), so I decided that it can help people in the future.

Instead of using the NSAttributed string, I encoded a variable containing HTML text in a fake URL. This makes mail.app treat it as an attachment, and I found that it shows its contents in the html <a> , as if it were an internal hyperlink.

Here is the code I used (in Swift):

  var eMailService:NSSharingService! = NSSharingService(named:NSSharingServiceNameComposeEmail) eMailService.subject = mySubject let message = "<html><body>Message body ...</body></html>" let embeddedMessage = "</a>" + message if let encodedMessage = embeddedMessage.stringByAddingPercentEncodingWithAllowedCharacters (NSCharacterSet.alphanumericCharacterSet()), let urlMessage = NSURL(string:encodedMessage)! { eMailService.performWithItems([urlMessage]) } 

Note1: the β€œ/ a” tag that I insert at the beginning of my inline message is designed to prevent the entire html content from being selected as a hyperlink (since mail.app puts my URL in the β€œa” tag, I just close it there).

Note2: this causes the NSSharingService to complain that the URL is not absolute (in the log), but it still correctly inserts it into the email. I can turn off this error by adding β€œdata:” before my inserted β€œ/ a” tag, but then β€œe-mail” appears in the email (which I didn't like).

+1
source

Source: https://habr.com/ru/post/972444/


All Articles