Creating an mailto: URL with a URL in the body iPhone SDK

Posted by Sjakelien on Stack Overflow See other posts from Stack Overflow or by Sjakelien
Published on 2009-07-03T13:40:44Z Indexed on 2010/05/07 4:08 UTC
Read the original article Hit count: 274

Filed under:
|
|
|

UPDATE: I just found a similar post here: http://stackoverflow.com/questions/730101/how-do-i-encode-in-a-url-in-an-html-attribute-value

Please consider the code below: I try to send an email message from within my iPhone app. The problem I encounter is that I want to put a URL in the body of the email. This URL contains two ampersands. I am encoding all the strings using "stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding", but the URL ends up in my new mail messagd truncated after the first ampersand. IE: "http://www.mydomain.nl/?cm=79&ctime=1246572000&cid=4772" becomes "http://www.mydomain.nl/?cm=79".

Any suggestion what I could do to escape?

 NSString *eMailSubject = @"My Subject";
 NSString *encodedSubject = [[NSString alloc] initWithString:[eMailSubject stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
 NSString *eMailBody = @"http://www.mydomain.nl?cm=79&ctime=1246572000&cid=4772";
 NSString *encodedBody = [[NSString alloc] initWithString:[eMailBody stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
 NSString *urlString = [[NSString alloc] initWithString:[NSString stringWithFormat:@"mailto:?subject=%@&body=%@", encodedSubject, encodedBody]];
 NSString *encodedURL = [[NSString alloc] initWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
 NSURL *url = [[NSURL alloc] initWithString:encodedURL];
 [[UIApplication sharedApplication] openURL:url];

© Stack Overflow or respective owner

Related posts about iphone

Related posts about mailto