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...