Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I encode "&" in a URL in an HTML attribute value?

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&param=0'>click me</a>"; //original
NSString *body = @"This is a link: <a href='http://somewhere.com/two.woa/wa?id=000&#38;param=0'>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];
like image 426
4thSpace Avatar asked Apr 08 '09 13:04

4thSpace


People also ask

What are examples of encoding?

For example, you may realize you're hungry and encode the following message to send to your roommate: “I'm hungry. Do you want to get pizza tonight?” As your roommate receives the message, they decode your communication and turn it back into thoughts to make meaning.

How do you encode a number?

The most common systems in computing are: Decimal : digit set = {0,1,2,3,4,5,6,7,8,9} , base = 10. Octal : digit set = {0,1,2,3,4,5,6,7} , base = 8. Hexadecimal : digit set = {0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F} , base = 16.


4 Answers

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

like image 54
Konstantinos Avatar answered Oct 11 '22 12:10

Konstantinos


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

like image 39
Chris Lundie Avatar answered Oct 11 '22 12:10

Chris Lundie


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

like image 29
Lucero Avatar answered Oct 11 '22 14:10

Lucero


you can simply use CFURLCreateStringByAddingPercentEscapes with CFBridgingRelease for ARC support

NSString *subject = @"This is a test";
// Encode all the reserved characters, per RFC 3986
// (<http://www.ietf.org/rfc/rfc3986.txt>)
NSString *encodedSubject =
(NSString *) CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                            (CFStringRef)subject,
                                            NULL,
                                            (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                            kCFStringEncodingUTF8));
like image 34
AbdullahDiaa Avatar answered Oct 11 '22 13:10

AbdullahDiaa