views:

2283

answers:

5

I'd like to make a URL click able in the email app. The problem is that a parameterized URL breaks this because of "&" in the URL. The body variable below is the problem line. Both versions of "body" are incorrect. Once the email app opens, text stops at "...link:". What is needed to encode the ampersand?

NSString *subject = @"This is a test";
NSString *encodedSubject = 
[subject stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

//NSString *body = @"This is a link: <a href='http://somewhere.com/two.woa/wa?id=000&amp;param=0'&gt;click me</a>"; //original
NSString *body = @"This is a link: <a href='http://somewhere.com/two.woa/wa?id=000&amp;#38;param=0'&gt;click me</a>"; //have also tried &amp;
NSString *encodedBody = 
[body stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
NSString *formattedURL = [NSString stringWithFormat: @"mailto:[email protected]?subject=%@&body=%@", encodedSubject, encodedBody];
NSURL *url = [[NSURL alloc] initWithString:formattedURL];
[[UIApplication sharedApplication] openURL:url];
+1  A: 

You can use a hex representation of the character, in this case %26.

Lucero
+5  A: 

the ampersand would be %26 for HEX in URL Encoding standards

Konstantinos
? there's no such thing as a decimal %-encoding; the ‘38’ is for numeric character references in [X][HT]ML.
bobince
+7  A: 

I've been using -[NSString gtm_stringByEscapingForURLArgument], which is provided in Google Toolbox for Mac, specifically in GTMNSString+URLArguments.h and GTMNSString+URLArguments.m.

Chris Lundie
FYI, you'll need these four files to get it to work:GTMDefines.hGTMGarbageCollection.hGTMNSString+URLArguments.hGTMNSString+URLArguments.m
Dave Gallagher
A: 
<a href='http://somewhere.com/two.woa/wa?id=000&amp;#38;param=0'&gt;click me</a>

Is correct, although ‘&amp;’ is more commonly used than ‘&#38;’ or ‘&#x2C;’.

If the ‘stringByAddingPercentEscapesUsingEncoding’ method does what it says on the tin, it should work(*), but the NSString documentation looks a bit unclear on which characters exactly are escaped. Check what you are ending up with, the URL should be something like:

mailto:[email protected]?subject=test&body=Link%3A%3Ca%20href%3D%22http%3A//example.com/script%3Fp1%3Da%26amp%3Bp2%3Db%22%3Elink%3C/a%3E

(*: modulo the usual disclaimer that mailto: link parameters like ‘subject’ and ‘body’ are non-standard, will fail in many situations, and should generally be avoided.)

Once the email app opens, text stops at "...link:".

If ‘stringByAddingPercentEscapesUsingEncoding’ is not escaping ‘<’ to ‘%3C’, that could be the problem. Otherwise, it might not be anything to do with escapes, but a deliberate mailer-level restriction to disallow ‘<’. As previously mentioned, ?body=... is not a reliable feature.

In any case, you shouldn't expect the mailer to recognise the HTML and try to send an HTML mail; very few will do that.

bobince
This was tagged as an iPhone question, and HTML mail works fine with iPhone Mail.
Chris Lundie
Yep - HTML mail works fine. I use it all the time. The problem is in the URL parameters.
4thSpace
+1  A: 

You use stringByAddingPercentEscapesUsingEncoding, exactly like you are doing.

The problem is that you aren't using it enough. The format into which you're inserting the encoded body also has an ampersand, which you have not encoded. Tack the unencoded string onto it instead, and encode them (using stringByAddingPercentEscapesUsingEncoding) together.

Peter Hosey
Wasn't really sure what you meant.
4thSpace
The format string you're passing to stringWithFormat: has an unencoded ampersand in it. stringWithFormat: won't escape it and your existing stringByAddingPercentEscapes message won't work backwards upon it.
Peter Hosey
I would rearrange the code so that you send stringByAddingPercentEscapes to the string you got from stringWithFormat:. The other way would be to escape it in the format string literal.
Peter Hosey