Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to present an MFMailComposeViewController in an already modalViewController?
    text
    copied!<p>I've a modalViewController with a button called email. On pressing this button it presents the MFMailComposeViewController modally. All works well when i send the email. But when the user taps the cancel button on the MFMail view the app hangs up and doesnt display the action sheet. It shows no details in the console etc, apparently the MFMailCompose delegate is not called in that case.</p> <p>I checked by using the same code in another view controller (which was not a modalviewcontroller) and found everything working perfectly there. Therefore, i assume that the issue is with presenting the MFMailComposeViewController in a controller thats already a modalviewcontroller.</p> <p>I've been browsing for some help since quite a long time and reached to the conclusion that since MFMailComposeViewController is itself a modalviewcontroller, therefore it can only be used on a simple view controller, and would not work in a modalviewcontroller.</p> <p>Can someone kindly guide me on how to use MFMailComposer in a modalviewcontroller. Any help would be highly appreciated.</p> <p>My Code:</p> <pre><code> -(IBAction)emailButtonPressed:(id)sender{ Class mailClass = (NSClassFromString(@"MFMailComposeViewController")); if (mailClass != nil) { if ([mailClass canSendMail]) { [self displayComposerSheet]; } else { [self launchMailAppOnDevice]; } } else { [self launchMailAppOnDevice]; } } #pragma mark - #pragma mark Compose Mail -(void)displayComposerSheet { MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; picker.mailComposeDelegate = self; [picker setSubject:@"Ilusiones"]; // Set up recipients NSArray *toRecipients = [NSArray arrayWithObject:@"anam@semanticnotion.com"]; [picker setToRecipients:toRecipients]; // Attach a screenshot to the email UIGraphicsBeginImageContext(self.view.bounds.size); [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); NSData *myData = UIImagePNGRepresentation(viewImage); [picker addAttachmentData:myData mimeType:@"image/png" fileName:@"viewImage"]; // Fill out the email body text NSString *emailBody = @""; [picker setMessageBody:emailBody isHTML:NO]; [self presentModalViewController:picker animated:YES]; [picker release]; } - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { NSLog (@"entered the delegate"); // this line is not shown when the cancel button is tapped-meaning it never enters the delegate in that case. switch (result) { case MFMailComposeResultCancelled: NSLog(@"Result: canceled"); break; case MFMailComposeResultSaved: NSLog(@"Result: saved"); break; case MFMailComposeResultSent: NSLog( @"Result: sent"); break; case MFMailComposeResultFailed: NSLog( @"Result: failed"); break; default: NSLog(@"Result: not sent"); break; } [self dismissModalViewControllerAnimated:YES]; //this works fine in the send case, and the email is sent. but hangs in the cancel case. } #pragma mark - #pragma mark Workaround -(void)launchMailAppOnDevice { NSString *recipients = @"mailto:anam@semanticnotion.com.com?cc=second@example.com,third@example.com&amp;subject=illusions!"; NSString *body = @"&amp;body=xyz"; NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body]; email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]]; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload