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¶m=0'>click me</a>"; //original
NSString *body = @"This is a link: <a href='http://somewhere.com/two.woa/wa?id=000&param=0'>click me</a>"; //have also tried &
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];
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.
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.
the ampersand would be %26 for HEX in URL Encoding standards
I've been using -[NSString gtm_stringByEscapingForURLArgument]
, which is provided in Google Toolbox for Mac, specifically in GTMNSString+URLArguments.h and GTMNSString+URLArguments.m.
You can use a hex representation of the character, in this case %26.
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With