Search code examples
iphoneemailgmail

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.


Solution

  • 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:@"[email protected]"];
    
    /* 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];