0

App is randomly crashing when mail account is not setup. Sometimes, I am getting error alert. Please help me.

if ([MFMailComposeViewController canSendMail]) {

        MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
        mc.mailComposeDelegate = self;
        [mc addAttachmentData:myData mimeType:@"application/pdf" fileName:@"myPdf2.pdf"];
        [self presentViewController:mc animated:YES completion:NULL];
    }

    else {

        UIAlertController * alert = [UIAlertController
                                     alertControllerWithTitle:@"Alert"
                                     message:@"Please set up your mail account!"
                                     preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction* okButton = [UIAlertAction
                                    actionWithTitle:@"OK"
                                    style:UIAlertActionStyleDefault
                                    handler:^(UIAlertAction * action) {
                                        //Handle your yes please button action here

                                    }];
        [alert addAction:okButton];
        [self presentViewController:alert animated:YES completion:nil];
    }
aBilal17
  • 2,974
  • 2
  • 17
  • 23
Catherine
  • 654
  • 5
  • 15
  • Do you have a crash log then? I guess that you don't pass the `[MFMailComposeViewController canSendMail]` when it happens, so the issue is on the Alert part? Is this your final code? Do you use popover style (can have issue on iPad if some setting are not done). Could it be because sometimes `myData` is nil (because it can't be according to the doc)? – Larme Apr 27 '18 at 09:22
  • I am getting this error "Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Engine marked as needing optimization even though it's already in progress." – Catherine Apr 27 '18 at 09:28
  • Please check this url and copy the code from there. https://stackoverflow.com/questions/4862523/objective-c-send-email-without-leaving-app I think you are not setting sender and body. – aBilal17 Apr 27 '18 at 09:32
  • I tried your url but I want to show some alert in else part if mail account is not setup @aBilal17 – Catherine Apr 27 '18 at 09:44
  • Issue is not in Alert part, issue is in if part, there please add sender and body. something like that. [mailCont setSubject:@"yo!"]; [mailCont setToRecipients:[NSArray arrayWithObject:@"joel@stackoverflow.com"]]; [mailCont setMessageBody:@"Don't ever want to give you up" isHTML:NO]; – aBilal17 Apr 27 '18 at 09:46

0 Answers0