Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid image relocation sending an email using NSSharingService with HTML content

I am trying to send an email using NSSharingService from a Mac app. I am including HTML code as email body. When the Mail window appears, the content is formatted, but the image in the HTML structure is moved to the bottom email. Also, even though I define this image as a link, the link is not performed in the email body.

This is the code I am 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 "Message body" text line and before "Video:...", but in my case it appears in the bottom.

The HTML code is tested on Firefox and Safari. Also, I can open the HTML text with Safari, and selecting "File->Share->Send this page by email" it does exactly what I am trying to do (but using Safari, not with my app).

Any idea is wellcome. Thanks in advance.

like image 440
goe Avatar asked Jul 18 '14 08:07

goe


2 Answers

I conducted a variety of tests using NSSharingService, including concatenating nsattributedstrings and using an NSTextAttachment, but it seems the NSSharingService (or NSAttributedString) always pushes the image to the end.

Try using the Apple Scripting bridge.

like image 198
danielmhanover Avatar answered Oct 05 '22 10:10

danielmhanover


This is a very old question but it seems no answer was found since it was asked and as of now (xCode 7 OSX 10.11), the problem is still present.

The solution I found is a bit of a hack but it keeps things simple and works (with mail.app) so I figured it could still help people in the future.

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

Here's 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 i'm inserting at the beginning of my embedded message is there to prevent the whole html content to be highlighted as an hyperlink (because mail.app places my URL inside an "a" tag, I just close it there).

Note2: this causes NSSharingService to complain about the URL not being absolute (in the log) but it still inserts it properly in the email. I could silence this error by adding "data:" ahead of my inserted "/a" tag but then "data:" appears on the eMail (which I didn't like).

like image 38
Alain T. Avatar answered Oct 05 '22 10:10

Alain T.