Search code examples
iosobjective-ciphonexcodemfmessagecomposeviewcontroller

At which case the MessageComposeResultFailed is fired for MFMessageComposeController in iOS?


The didFinishWithResult delegate method of MFMessageComposeViewController shows that the message has been sent successfully, even when the device is in airplane mode or when the device has no SIM,but the message sending is failed.The delegate does not go through the failure state.Why doesnt it go to failure state?.Please give me some suggestion on this. The code is given as below:

    MFMessageComposeViewController *pic = [[MFMessageComposeViewController alloc] init];
    pic.messageComposeDelegate = self;
    
    // You can specify one or more preconfigured recipients.  The user has
    // the option to remove or add recipients from the message composer view
    // controller.
    /* picker.recipients = @[@"Phone number here"]; */
    
    // You can specify the initial message text that will appear in the message
    // composer view controller.
    pic.body = @"Hello from California!";
    
    [self presentViewController:pic animated:YES completion:NULL];
}

And the DELEGATE methods are given below:

  // -------------------------------------------------------------------------------
//  messageComposeViewController:didFinishWithResult:
//  Dismisses the message composition interface when users tap Cancel or Send.
//  Proceeds to update the feedback message field with the result of the
//  operation.
// -------------------------------------------------------------------------------
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller 
              didFinishWithResult:(MessageComposeResult)result
{
    
    // Notifies users about errors associated with the interface
    switch (result)
    {
        case MessageComposeResultCancelled:
//Cancelled
            break;
        case MessageComposeResultSent:
//Sent
            break;
        case MessageComposeResultFailed:
//Failed
            break;
        default:
            
            break;
    }
    
    [self dismissViewControllerAnimated:YES completion:NULL];
}

For me when I press "CANCEL" or "SEND" message the correct delegates are fired. But when I switch ON the AIRPLANE Mode or the device with no SIM still the MessageComposeResultSent Is fired. Can some one tell clearly when the MessageComposeResultFailed is fired? Any live steps? Please kindly help me


Solution

  • This behavior makes sense based on the docs for MessageComposeResult:

    case sent
       The user successfully queued or sent the message.
    

    The delegate method can report MessageComposeResultSent even if all your app did was give the message to the system to send. It's not a guarantee that it actually sent the message.

    The docs aren't clear about when it reports MessageComposeResultFailed, but I imagine it's meant to capture any possible error that could occur.

    Depending on your particular use case, you could add an check for cellular connectivity before allowing someone to use MFMessageComposeViewController.