Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MFMailComposeViewController not displaying in iOS 8

Tags:

ios

When I run Xcode 6 and use the iOS 8 simulator, I am trying to load a MFMailComposeViewController, it does not come up. Instead I get this error...

"Warning: Attempt to present MFMailComposeViewController: 0x7c30c400 on ViewController: 0x7baf6000 which is already presenting (null)"

This exact same code has been working for almost a year now, unchanged. Just to be sure, I ran my app in Xcode 6 with the iOS 7.1 simulator and it worked like before, perfectly.

Does anyone know how to display a MFMailComposeViewController view in iOS 8? This seems like a simple task and I'm sure its an easy fix.

PS. Posting is NOT needed because it is standard code but here it is anyway...

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];

picker.mailComposeDelegate = self;

//Current Date
NSDateFormatter *formatter = [[NSDateFormatter alloc]  init];
NSDate  *date = [NSDate date];
//Ensure UTC DATE
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[formatter setDateFormat:@"MM/dd/yyyy 'at' HH:mm:ss UTC"];
NSString *myString = [formatter stringFromDate:date];

NSString *emailBody = [NSString stringWithFormat:@"Feedback time at %@.<br><br>Name:", myString];
[picker setMessageBody:emailBody isHTML:YES];

NSString *subject, *emailAddress;

[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[formatter setDateFormat:@"MM/dd/yyyy"];
subject = [NSString stringWithFormat:@"Feedback"];

//Set the Subject
[picker setSubject:subject];

emailAddress = @"[email protected]";

//Set the Receivers
NSArray *myReceivers = [[NSArray alloc] initWithObjects:emailAddress, nil];
[picker setToRecipients:myReceivers];

//It saves the file name as the same as Subject here!
picker.navigationBar.barStyle = UIBarStyleDefault;//UIBarStyleBlack;
[self presentViewController:picker animated:YES completion:^{}];
like image 484
Airtower Avatar asked Sep 22 '14 15:09

Airtower


3 Answers

I had a similar issue on iOS 8, which worked fine on iOS 7 and before.

I presented my MFMailComposeViewController after user press a button in UIActionSheet. So, in previous implementation, I put the mail composer and show MFMailComposeViewController in UIActionSheetDelegate function:

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

which was straightforward with the function name.

But, maybe caused by some UI animation conflict, showing MFMailComposeViewController and dismissing UIActionSheet at the same time makes MFMailComposeViewController not shown.

  • Solution

My solution is putting MFMailComposeViewController showing in UIActionSheetDelegate function:

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex

Which is called after the UIActionSheet animation.

Wish this could help someone who has the same problem.

like image 165
Charlie Hung Avatar answered Oct 22 '22 08:10

Charlie Hung


I was having a similar problem but did not get the same message. However on iOS 8 my Mail Composer would not display. I was launching it from a button on a UIPopover. I would show the Mail Composer and then a few lines of code later I called:

    [myPopover dismissPopoverAnimated:YES];

Another thread on StackOverflow indicated that maybe it had something to do with another view controller being active. So I moved my dismiss popover above the other call to show the Mail Composer and also made it not animated:

    [aboutPopover dismissPopoverAnimated:NO];

Now it works.

like image 20
Ty Jacobs Avatar answered Oct 22 '22 06:10

Ty Jacobs


I don't know why this worked. I am not presenting any other viewControllers prior to calling this method but if I dismissed viewControllers first it works...

[self dismissViewControllerAnimated:YES completion:^{
    [self presentViewController:picker animated:YES completion:^{}];
}];
like image 1
Airtower Avatar answered Oct 22 '22 07:10

Airtower