0

I am trying to launch the MFMailComposer on the iOS 7 simulator and as soon as it comes up it immediately dissmises itself and I get the following error in the debugger.

_serviceViewControllerReady:error: Error Domain=NSCocoaErrorDomain Code=4097 "The     operation couldn’t be completed. (Cocoa error 4097.)"

Here is my code

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    switch (buttonIndex) {
        case 0:
            break;
        case 1:{
            if ([MFMailComposeViewController canSendMail]) {
                MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
                mailViewController.mailComposeDelegate = self;
                [mailViewController setSubject:[NSString stringWithFormat:@"%@ Totals Report",_teamStats.relationshipTeam.teamName]];
                [mailViewController setMessageBody:@"\n\n\n\n\nSent From HoopMetrics" isHTML:NO];
                // Attach a doc to the email

                NSData* data =  [_teamStats.relationshipTeam  pdfDataFromString:_teamStats.teamTotalsAsString];
                [mailViewController addAttachmentData:data mimeType:@"application/pdf" fileName:@"Totals Report"];
                [self presentViewController:mailViewController animated:YES completion:nil];

            }
            else{
                HMAlertView*alert = [[HMAlertView alloc]initWithTitle:@"No Email" message:@"Please, set up an email account on your device" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                [alert show];
            }
        }
            break;
}
Luke
  • 612
  • 1
  • 6
  • 19
  • check this question and answer http://stackoverflow.com/questions/18978864/cannot-show-modal-viewcontroller-in-ios7 – Khash Nejad Nov 13 '13 at 08:18

2 Answers2

2

I faced the same problem. Just turn off appearance customization for UINavigationBar or UIBarButtonItem or some another element that MFMailComposeViewController may use.

Yanny
  • 490
  • 4
  • 16
  • In my case it was -[[UIBarButtonItem appearance] setBackButtonBackgroundImage:forState:barMetrics:] that was causing this. Other appearance customizations did not seem to be a problem. – Ryan Feb 07 '14 at 23:55
  • 1
    In my case it was due to `[[UITableView appearance] setSeparatorInset:UIEdgeInsetsZero]`. – fabb Feb 27 '14 at 15:56
0

You can get around this with the following hack using performSelector:

- (IBAction)sendEmail:(id)sender {
  [self performSelector:@selector(showEmailComposer) withObject:nil afterDelay:0.0];

}

-(void) showEmailComposer{


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

  //Set recipients...other stuff
  [mailViewController setToRecipients:recipients];
  [mailViewController setSubject:subject];
  [mailViewController setMessageBody:body isHTML:isHTML];


  mailViewController.title = @"Email VC Title";


  mailViewController.mailComposeDelegate = delegate;
  [self presentViewController:mailViewController
                     animated:YES
                   completion:NULL];

}
Mariam K.
  • 600
  • 4
  • 13