1

I just tried to send email using this code and it seems to work:

NSString* urlEmail = [NSString stringWithString: @"mailto:foo@example.com?cc=bar@example.com&subject=test%20from%20me!&body=this%20is%20a%20test!"];

[[UIApplication sharedApplication] openURL: [NSURL URLWithString: urlEmail]];

The only problem is, is there a function I can use to automatically escape everything in a normal NSString so I don't have to write it out manually like this? If not, how can I use stringWithFormat without conflicting with the % signs already in the string? I basically want to be able to append to, subject, body, etc. dynamically.

user229044
  • 232,980
  • 40
  • 330
  • 338
gonzobrains
  • 7,856
  • 14
  • 81
  • 132

2 Answers2

9

Use MFMailComposeViewController:

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setToRecipients:recipients];
[picker setSubject:subject];
[picker setMessageBody:body isHTML:YES];
[self presentModalViewController:picker animated:YES];
[picker release];
nebulabox
  • 430
  • 3
  • 4
1

According to https://stackoverflow.com/a/917630/535632, you can escape like this:

NSString *urlStr = [urlEmail stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Community
  • 1
  • 1
Jayson Lane
  • 2,828
  • 1
  • 24
  • 39
  • Also, this answer (same question) shows handling of additional characters: http://stackoverflow.com/a/4140183/535632 – Jayson Lane Mar 15 '12 at 03:04