7

How do I show the mail setup page programmatically?

In my app, I offered a feedback option to the user. While tapping on the feedback button, I check whether is there any mail account available in the device or not. This is done with the following check:

if ([MFMailComposeViewController canSendMail])
{
    // Actions to send mail
}
else
{
    //Actions to show an error message by UIAlertView
}

The alert message will be like this:

No mail account alert

If the user taps on the OK button in this UIAlertView, I want to go to the mail setup page available in the settings menu. That is, I want to show the following page:

mail setup page

Is it possible to do this navigation programmatically?

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192

6 Answers6

6

When the user clicks on the alert view 'OK' button, use the below code.

[[UIApplication sharedApplication] openURL: [NSURL URLWithString: @"mailto:test@test.com"]];

This will open the native Mail application home page allowing the user to add a new mail account.

Hopes this helps :)

Kousik
  • 21,485
  • 7
  • 36
  • 59
Basheer
  • 1,207
  • 9
  • 10
  • But first test to make sure that URL can be opened. The user may have not have the ability to open Mail with parental controls on. In that case Rajkanth is really stuck. – fearmint Sep 22 '12 at 18:01
  • 2
    just a small correction for the code to really work - [[UIApplication sharedApplication] openURL: [NSURL URLWithString: @"mailto:test@test.com"]] – Kostiantyn Sokolinskyi Sep 22 '13 at 16:16
2

You have to use the MFMailComposeViewController class, and the MFMailComposeViewControllerDelegate protocol,

PeyloW provides the following code for this in his answer here:

First to send a message:

MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:@"My Subject"];
[controller setMessageBody:@"Hello there." isHTML:NO]; 
[self presentModalViewController:controller animated:YES];
[controller release];

Then the user does the work and you get the delegate callback in time:

- (void)mailComposeController:(MFMailComposeViewController*)controller  
          didFinishWithResult:(MFMailComposeResult)result 
                        error:(NSError*)error;
{
  if (result == MFMailComposeResultSent) {
    NSLog(@"It's away!");
  }
  [self dismissModalViewControllerAnimated:YES];
}
Community
  • 1
  • 1
Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76
  • Thanks for your reply... This is the procedure to send a mail using MFMailComposeViewController. It will work when you previously set an account by going Settings->Mail,Contacts,Calenders->Add account. It won't work if you did not set an account previously in your device. Am i right? So we should show some kind of messages to the user to indicate to set the mail account by using UIAlertView. What i want is, after showing that message in UIAlertView, if the user hits the OK button of the alert view, the setup page which i mentioned above should automatically come in front of the user.. –  May 16 '11 at 10:31
  • If you haven't set up a mail account on your device then this will show the Mail setup view before it lets you send a message (as far as I'm aware) – Thomas Clayson May 16 '11 at 10:36
  • You really should indicate that the source for your code is PeyloW's answer here: http://stackoverflow.com/questions/310946/how-can-i-send-mail-from-an-iphone-application/1513433#1513433 – Brad Larson May 17 '11 at 17:57
0

-(IBAction)showPicker:(id)sender {

    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
    if (mailClass != nil)

    {

        // We must always check whether the current device is configured for sending emails

        if ([mailClass canSendMail])
        {

            [self displayComposerSheet];

        }
        else

        {
            [self launchMailAppOnDevice];
        }
    }
    else
    {
        //mail not config
    }
}
Priyan Haridas
  • 183
  • 2
  • 7
0

Add the messageUI framework. in .h file

#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
 add <MFMailComposeViewControllerDelegate> like
@interface ManageRequestViewController : UIViewController<MFMailComposeViewControllerDelegate>

in .m file

if([MFMailComposeViewController canSendMail]){

            MFMailComposeViewController *mail=[[MFMailComposeViewController alloc]init];
            mail.mailComposeDelegate=self;
            [mail setSubject:@"your subject"];          
            [mail setMessageBody:@"mail!" isHTML:NO];
            [self presentModalViewController:mail animated:YES];
            [mail release];         
        }

- (void)mailComposeController:(MFMailComposeViewController*)mailController didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
        [self dismissModalViewControllerAnimated:YES];

}
Henrik P. Hessel
  • 36,243
  • 17
  • 80
  • 100
Gypsa
  • 11,230
  • 6
  • 44
  • 82
  • This is for sending mail... My question is how to display the mail account setup page from settings –  May 16 '11 at 10:57
0

The short answer to your specific question is that it is not possible to programmatically enable mail account creation using the iOS SDK.

Saurabh G
  • 1,895
  • 14
  • 15
  • I don't want to add an account programmatically. I just want to show that setup page. So that, the user need not go there manually –  May 16 '11 at 10:55
0

Can't be done. Even if there's an interface to launch the Settings app (which I'm not aware if there is), there's no way to specify which screen of that app to go to. This isn't like a website, where every page has a URL.

Dan Ray
  • 21,623
  • 6
  • 63
  • 87