views:

2500

answers:

2

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];
A: 

Is the body of the email message HTML? If so, most email readers and browsers will try to interpret the ampersand as an HTML entity prefix and seeing 'ctime' instead of a legal entity value following the ampersand, just give up on parsing the rest of the string.

Here are some details. Try doing a string substitution of the '&' characters with '&' and see if that helps.

Also, might want to consider using the new messaging framework in OS 3.0 that allows you to send email directly from inside your app.

Ramin
Sjakelien
Ramin
A: 

If you want real HTML URLs you can't use the mailto:.

You have to do one of the following:

  1. implement your own SMTP server
  2. use SDK 3.0 mail framework (MFMailCompose...)
Kailoa Kadano
That is quite a statement! Do you have some argumentation for it?
Sjakelien
mostly trial and error. In the iPhone 2.2 days, I released some code to do the smtp part. it's avalable at: http://github.com/kailoa/iphone-smtp/tree
Kailoa Kadano