Faheem Rajput
Faheem Rajput

Reputation: 601

How to open iphone mail application from my own application?

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?

Upvotes: 20

Views: 26257

Answers (4)

Alessandro Pirovano
Alessandro Pirovano

Reputation: 2551

stringByAddingPercentEscapesUsingEncoding and openURL are deprecated.

Now use this:

#define URLEMail @"mailto:[email protected]?subject=title&body=content"

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

UIApplication *application = [UIApplication sharedApplication];
    [application openURL:[NSURL URLWithString: encodedString] options:@{} completionHandler:nil];

Upvotes: 8

adali
adali

Reputation: 5977

#define URLEMail @"mailto:[email protected]?subject=title&body=content"

 NSString *url = [URLEMail stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding ]; 
 [[UIApplication sharedApplication]  openURL: [NSURL URLWithString: url]];

Upvotes: 47

Mick MacCallum
Mick MacCallum

Reputation: 130222

Try this out.

-(void)launchMailAppOnDevice
{
    NSString *recipients = @"mailto:[email protected]?subject=subjecthere";
    NSString *body = @"&body=bodyHere";

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

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}

Upvotes: 11

Zack Brown
Zack Brown

Reputation: 6028

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.

Upvotes: 4

Related Questions