0

I am struggling with view hierarchy but I cannot figure out what is going wrong here. I try to send a message (MFMessageComposeViewController). I would like to show an alertView depending on the success (or not) with the help of delegate methods. The alertView is displayed when the message is cancelled but I get this error when the message is sent:

Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior (<UIAlertController: 0x153b3c00>)

It looks like as if the completion block was called before the MFMessageComposeView was completey dismissed (or deallocated) when the message is sent (as if there was still something running behind the scenes), but not when it is cancelled. If it can help:

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {

    int tag;

    switch (result) {
        case MessageComposeResultCancelled:
        {
            tag = 1;
            NSLog(@"Cancelled");
            break;
        }
        case MessageComposeResultFailed:
        {
            tag = 2;
            NSLog(@"Failed");
            break;
        }

        case MessageComposeResultSent:
        {
            tag = 3;
            NSLog(@"Sent");
            break;
        }
        default:
            break;
    }
    [controller dismissViewControllerAnimated:YES completion:^{
        [self alertSMSMessageWithTag:tag];
    }];
}

- (void)alertSMSMessageWithTag:(int)tag {

    UIAlertController *alertController;

    if (tag == 1) { // cancelled
        alertController = [UIAlertController alertControllerWithTitle:@"Message cancelled" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
        [alertController addAction:ok];
    }
    if (tag == 2) { // Failed
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Message failed" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
        [alertController addAction:ok];
    }
    if (tag == 3) { // sent
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Message sent" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
        [alertController addAction:ok];
    }
    [self presentViewController:alertController animated:YES completion:nil];
}

If anyone could help me...

Trichophyton
  • 625
  • 5
  • 21

1 Answers1

0

You are not initialising UIAlertController in a proper way. You are using same variable name alertController which is public to

if (tag == 3) {
}

but not public to the method. And you are using same variable name which is public to method UIAlertController *alertController; [self presentViewController:alertController animated:YES completion:nil]; is accessing public to method variable i.e UIAlertController *alertController; which you have correctly initialised for tag 1. I would suggest you to use following code -

- (void)alertSMSMessageWithTag:(int)tag {

     UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:ok];
    if (tag == 1) { // cancelled
       alertController.title = @"Message cancelled";

    }
    if (tag == 2) { // Failed
        alertController.title = @"Message failed";
    }
    if (tag == 3) { // sent
       alertController.title = @"Message sent";

    }
    [self presentViewController:alertController animated:YES completion:nil];
}

Hope its helpful.

CST
  • 313
  • 2
  • 10