19

i am working on signup feature. In this feature when the user create account successfully. i am asking him or her to activate his account. i want to open the mail application of iphone if user say yes. now my question is simple how to open mail application from my own application?

Faheem Rajput
  • 591
  • 4
  • 8
  • 17
  • http://stackoverflow.com/questions/1942689/iphone-email-app-launch-url – Rok Jarc May 09 '12 at 12:08
  • 1
    There's an excellent answer [here](http://stackoverflow.com/a/24953719/2547229) that links to [this article](https://medium.com/@vijayssundaram/how-to-deep-link-to-ios-7-mail-6c212bc79bd9). You can directly open the App, and you can even open a specific email (provided Apple don't remove the API, anyway). – Benjohn Jul 09 '15 at 13:03
  • If somebody would like to open compose email inside app http://stackoverflow.com/questions/4862523/objective-c-send-email-without-leaving-app – andilabs Aug 15 '15 at 21:58

4 Answers4

47
#define URLEMail @"mailto:sb@sw.com?subject=title&body=content"

 NSString *url = [URLEMail stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding ]; 
 [[UIApplication sharedApplication]  openURL: [NSURL URLWithString: url]];
adali
  • 5,977
  • 2
  • 33
  • 40
  • Exactly what I was looking for, thanks! Just FYI, your code has a semantical error. I think you meant to say `NSString *url = [URLEMail stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];` – dooleyo Oct 22 '13 at 17:45
  • Any tips on how to try this in the simulator? – Rudolf Real Apr 30 '15 at 18:30
11

Try this out.

-(void)launchMailAppOnDevice
{
    NSString *recipients = @"mailto:myemail@gmail.com?subject=subjecthere";
    NSString *body = @"&body=bodyHere";

    NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
    email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
7

stringByAddingPercentEscapesUsingEncoding and openURL are deprecated.

Now use this:

#define URLEMail @"mailto:sb@sw.com?subject=title&body=content"

NSString * encodedString = [URLEMail stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];

UIApplication *application = [UIApplication sharedApplication];
    [application openURL:[NSURL URLWithString: encodedString] options:@{} completionHandler:nil];
Alessandro Pirovano
  • 2,509
  • 28
  • 21
4

Ahoy!

The long and short of it is; you can't.

You can create an email compose view for the purpose of sending emails (see MFMailComposeViewController), but you cannot open applications arbitrarily without a purpose.

See this previous post for clarification: Launch an app from within another (iPhone)

Really though, it's not much effort for the user to close your app and open Mail so I wouldn't worry too much about it anyway.

Community
  • 1
  • 1
Zack Brown
  • 5,990
  • 2
  • 41
  • 54