Skip to content

Programmatically show Mail settings in iOS

MFMailComposerViewController can’t handle a device without a mail account defined, a easy and nice way to get around it is to do like this:

if ([MFMailComposeViewController canSendMail])
  MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];
  mailController.mailComposeDelegate = (id<MFMailComposeViewControllerDelegate>) self;
  [mailController setMessageBody:@"Test Mail" isHTML:YES];
  [self presentModalViewController:mailController animated:YES];
  NSString *bodyText = @"Test Mail";
  NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"mailto:?", 
      [bodyText stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]];

  [[UIApplication sharedApplication] openURL:url];

By using the “mailto:” url, we can open the mail app and it will show the settings view where the user can add a account, and by specifying body (we can also specify subject) text we get a nice pre-filed in email ready to be sent.

Post a Comment

Your email is never published nor shared. Required fields are marked *