0

I am developing an app that allows users to take pictures and send them by mail (xcode version 5.1.1). After the mail is sent, a confirmation message pops up:

   - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error{
    switch (result)
    {
    case MFMailComposeResultCancelled:
        [[[UIAlertView alloc]initWithTitle:@"Message Cancelled" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]show];
        break;
    case MFMailComposeResultSent:
        [[[UIAlertView alloc]initWithTitle:@"Message Sent" message:@"Thank you for your help." delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]show];                 break;
    default:
        break;
    }
    [self dismissViewControllerAnimated:NO completion:nil];
}

On click of "OK" in the simulator, Xcode highlights a code in the main.m file, with the phrase "Thread 1: signal SIGABRT":

int main(int argc, char * argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

When I test the app on the iPhone, same thing, it crashes on click of OK.

Do you have any idea on how to solve this issue?

Many thanks in advance for your help and advice

rmaddy
  • 314,917
  • 42
  • 532
  • 579
mdicamp
  • 25
  • 5
  • Try adding an exception breakpoint and reproducing the crash: http://stackoverflow.com/questions/17802662/exception-breakpoint-in-xcode – BergQuester Jul 11 '14 at 16:24
  • I can't reproduce your problem. With your delegate method in my test the alertView is showing. You declared the `MFMailComposeViewControllerDelegate` to your viewController? Please show how you call the `MFMailComposeViewController` and setup the mail. – Sebastian Jul 11 '14 at 16:46
  • 1
    To learn how to debug, see http://www.raywenderlich.com/10209/my-app-crashed-now-what-part-1 – rmaddy Jul 11 '14 at 16:47
  • And you should be calling `[controller dismissViewControllerAnimated:YES completion:nil];`. – rmaddy Jul 11 '14 at 16:47
  • change the delegate of alertview to nil in place of self. – Pawan Rai Jul 11 '14 at 16:54
  • @ManishSharma That's pointless. 1) controller won't be `nil`. 2) If it was, there can't be a problem calling a method on a `nil` pointer. That's a no-op in Objective-C. – rmaddy Jul 11 '14 at 16:55

2 Answers2

3

The issue is most likely caused by the fact that you set the alert view's delegate to self and then you dismiss self. When you then tap OK on the alert view, it attempt to access its delegate but the delegate was dismissed so the app crashes.

There are two fixes:

  1. Pass nil to the delegate parameter when creating the alert views. You have no need to process any alert view actions.
  2. When dismissing the mail controller, do it by as follows:

code:

[controller dismissViewControllerAnimated:YES completion:nil];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
0

If I am interpreting your question correctly, it is after you display the UIAlertView, and the user clicks on "OK" to dismiss your UIAlertView that you get the crash. If so, then the crash is most likely happening in your UIAlertView delegate inside:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
}

Try setting a breakpoint near the top of that method, then step through the code until the error occurs (after you press "OK").

In there you will probably find that you are attempting to access an object that no longer exists. If you cannot locate the error there, then it would be best to post the code for your didDismissWithButtonIndex: method.

user2338215
  • 831
  • 7
  • 11