简体   繁体   中英

Open mail client of iPhone programmatically

In my application, if the user gave their gmail account then i am required to open the mail client with the gmail login credentials which comes when we select gmail option of mail programmatically but if that account is already stored in mail then i am required to redirect the user directly to their account. Can anybody pliz give me a glimpse of how i can achieve this programmatically.

You won't get that much control over the Mail app as all apps on the iPhone are sandboxed to prevent them from messing with Apple applications.

The only thing you can do (if you want to open the mail client to send an email), is something like this:

/* create mail subject */
NSString *subject = [NSString stringWithFormat:@"Subject"];

/* define email address */
NSString *mail = [NSString stringWithFormat:@"test@test.com"];

/* define allowed character set */
NSCharacterSet *set = [NSCharacterSet URLHostAllowedCharacterSet];

/* create the URL */
NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"mailto:?to=%@&subject=%@",
                                                                                        [mail stringByAddingPercentEncodingWithAllowedCharacters:set],
                                                                                        [subject stringByAddingPercentEncodingWithAllowedCharacters:set]]];    
/* load the URL */
[[UIApplication sharedApplication] openURL:url];

/* release the URL. If you are using ARC, remove this line. */
[url release];

Swift:

        if let url = NSURL(string: "mailto://\(email)") {
            UIApplication.sharedApplication().openURL(url)
        }

Swift version of Léon Rodenburg's answer:

    // define email address
    let address = "test@test.com"

    // create mail subject
    let subject = "Subject"

    // create the URL
    let url = NSURL(string: "mailto:?to=\(address)&subject=\(subject)".stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!)

    // load the URL
    UIApplication.sharedApplication().openURL(url!)

I would suggest a much more improved answer. The Slack.com mobile app does this, it detects common email clients listed on the device and shows a popup picker of 'which' email client you would like to open.

So to implement:

  1. Google around to find the top 10 email clients (eg Mail, Google Inbox, OutLook, AirMail etc).

  2. Get a list of installed apps on the phone either by searching all apps (but I am told you can now only find if an app is explicitly installed, so you will need detect the app).

  3. Show a popup list if more than 1 email app is detected, requesting them 'which' app to open eg. Mail, Inbox.

This is the best solution I have seen working to date.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM