Reconquistador
Reconquistador

Reputation: 895

Detecting that SMS failed to be sent on iOS

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?

Upvotes: 1

Views: 950

Answers (1)

Mayank Patel
Mayank Patel

Reputation: 3908

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];
}

Upvotes: 1

Related Questions