How do I show the mail setup page programmatically

2020-07-10 09:05发布

问题:

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:

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:

Is it possible to do this navigation programmatically?

回答1:

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 :)



回答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];
}


回答3:

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];

}


回答4:

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



回答5:

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.



回答6:

-(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
    }
}


标签: iphone email