1

In my app, it is very important to know whether SMS has been sent or not. For checking, I am using this delegate method:

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

    switch (result) {
        case MessageComposeResultCancelled: {
            [NSThread detachNewThreadSelector:@selector(SMScancelled) toTarget:self withObject:nil];
        }
            break;
        case MessageComposeResultSent: {
            [NSThread detachNewThreadSelector:@selector(SMSsent) toTarget:self withObject:nil];
        }
            break;
        case MessageComposeResultFailed: {
            [NSThread detachNewThreadSelector:@selector(SMSfailed) toTarget:self withObject:nil];
        }
            break;
        default:
            break;
    }

    [self dismissViewControllerAnimated:YES completion:nil];
}

My problem is, that when testing, I turn on Airplane mode in settings (to test what will happen) and then I am trying to send SMS (using my app). Naturally, iOS fails to send it and system informs me about that. In message app, it is also shown, that I have failed to send it. But delegate method still returns MessageComposeResultSent instead of MessageComposeResultFailed. This situation also happens when I test on another phone that has no SIM card.

I am testing this on iOS 7 and iOS 8.

In documentation, there is written, that MessageComposeResultSent means "The user successfully queued or sent the message". This means, behaviour I am expecting is correct.

So how to know, whether my last SMS has been succesfully sent, or that sending has failed?

Reconquistador
  • 885
  • 8
  • 28
  • Were you able to check it ? I am also facing the same issue. Any idea when this delegate MessageComposeResultFailed gets fired? – User1075 Jan 16 '21 at 10:30
  • Sadly, I do not remember, as it happened 5 years ago and now I am no longer iOS developer, so I am not able to test it anymore. – Reconquistador Jan 18 '21 at 12:13

1 Answers1

1

You can verify if the device is allowed to send text message by using the canSendText method of the MFMessageComposeViewController.

Add this below code when you care sending the message (This method detects of your device does not support SMS)

if(![MFMessageComposeViewController canSendText]) {
    UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Your device doesn't support SMS!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [warningAlert show];
    return;
}

You can check Message failed by using delegate method of MFMessageComposeViewController

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult) result
{
    switch (result) {
       case MessageComposeResultCancelled:
       break;

       case MessageComposeResultFailed:
       {
            UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to send SMS!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [warningAlert show];
            break;
       }

       case MessageComposeResultSent:
           break;

       default:
           break;
   }

   [self dismissViewControllerAnimated:YES completion:nil];
}
Mayank Patel
  • 3,868
  • 10
  • 36
  • 59
  • Thank you, I know about method canSendText, but this alone is not enought. I can test it before opening of MFMessageComposeView and it can return TRUE, but when the user will attempt to send SMS, device can loose signal and will fail sending. I am using messageComposeViewController:didFinishWithResult, but its behaviour is not as I need (it is mentioned in answer). – Reconquistador Jan 20 '16 at 08:34
  • what you want then ? if device losses the signal then it will return `MessageComposeResultFailed` – Mayank Patel Jan 20 '16 at 08:39
  • If that would be true, why it is returning MessageComposeResultSent, when I try to send when Airplane mode is on? Or is this different behaviour than loosing signal? – Reconquistador Jan 20 '16 at 08:46
  • I am not sure why you are getting sent but I getting correct output – Mayank Patel Jan 20 '16 at 08:48
  • It might be due to different iOS. I am trying this on iOS 8 (do not have access to iOS 9 device today). – Reconquistador Jan 20 '16 at 08:52
  • hmm might be possible, it looks like bug of iOS you can upgrade to new version will fixes your problem – Mayank Patel Jan 20 '16 at 08:53
  • This will only hide it, as there are still iOS 8 users (over 7% of users in another app) that fill get wrong information. – Reconquistador Jan 20 '16 at 08:58
  • When the delegate is called with a value of `MessageComposeResultSent`, it just means that the user chose to send. Subsequent failure isn't reported. – Avi Jan 20 '16 at 10:44
  • @MayankPatel I too face the issue ... in iOS 13 too if I am putting Airplane mode on or disabling the SIM, I get 'MessageComposeResultSent' . can you please tell when this MessageComposeResultFailed will be fired? – User1075 Jan 16 '21 at 10:03
  • @Fionashoff it went to outbox and when you connect with the internet again then that mail will be delivered that is how it should work. – Mayank Patel Jan 16 '21 at 10:33
  • @MayankPatel But I am not talking about mail / I am talking about SMS.. So at which case MessageComposeResultFailed will be fired. I am behind this for few days. I just need a test case to make testing team understand – User1075 Jan 16 '21 at 10:35
  • @MayankPatel I am using MFMessage controller which is different from Mail. – User1075 Jan 16 '21 at 10:37