gonzobrains
gonzobrains

Reputation: 8036

iOS 5 simplest way to send email from an app

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

NSString* urlEmail = [NSString stringWithString: @"mailto:[email protected][email protected]&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.

Upvotes: 1

Views: 4284

Answers (2)

nebulabox
nebulabox

Reputation: 430

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

Upvotes: 9

Jayson Lane
Jayson Lane

Reputation: 2818

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

NSString *urlStr = [urlEmail stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

Upvotes: 1

Related Questions