1

I made a basic app that for now just has a button that you click, and it brings up the sms composer (ios7 and xcode 5). I think I've handled everything well. The simulator doesn't support sending messages, so I tried on my phone, but the message never actually sends. You can click the send button and cancel button fine, but again, the message never sends. Any ideas? Here is my code:

- (IBAction)text:(UIButton *)sender {
    MFMessageComposeViewController *messageVC = [[MFMessageComposeViewController alloc] init];
    [messageVC setMessageComposeDelegate:self];
    if ([MFMessageComposeViewController canSendText]) {

        NSString *smsString = [NSString stringWithFormat:@"message to send"];
        messageVC.body = smsString;
        messageVC.recipients = @[@"number to send to..."];
        messageVC.messageComposeDelegate = self;
        [self presentViewController:messageVC animated:YES completion:nil];
    }
}

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    switch (result) {
        case MessageComposeResultCancelled:
            NSLog(@"Message was cancelled");
            [self dismissViewControllerAnimated:YES completion:NULL];             break;
        case MessageComposeResultFailed:
            NSLog(@"Message failed");
            [self dismissViewControllerAnimated:YES completion:NULL];             break;
        case MessageComposeResultSent:
            NSLog(@"Message was sent");
            [self dismissViewControllerAnimated:YES completion:NULL];             break;
        default:             
            break;     
    } 
}

EDIT: So I tried it this morning (I did nothing to it overnight) and it worked. Not sure what the issue was. Thanks though!

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • As an aside, your switch statement needs 3 more break statements, one per case, but I don't think that's causing your problem. – EricS Jul 04 '14 at 02:33
  • 1
    @EricS Hilariously, they are there just with a huge amount of whitespace... – borrrden Jul 04 '14 at 02:59
  • Did you check in which switch case it lies during execution inside delegate function? – NeverHopeless Jul 04 '14 at 03:45
  • Yeah formatting didn't turn out well... And I did. It prints out "Message was sent", so it seems to be going down the right path. Just not actually delivering the message – user3754489 Jul 04 '14 at 04:16

2 Answers2

0

I assume the messageVC.recipients is in correct format (array of numbers)?

Try:

//check if the device can send text messages
if(![MFMessageComposeViewController canSendText]) {
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Your device cannot send text messages" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    return;
}

//set receipients
NSArray *recipients = [NSArray arrayWithObjects:@"0912345679",@"0923456790",@"0934567901", nil];

//set message text
NSString * message = @"this is a test sms message.";

MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init];
messageController.messageComposeDelegate = self;
[messageController setRecipients:recipients];
[messageController setBody:message];

// Present message view controller on screen
[self presentViewController:messageController animated:YES completion:nil];
joelrb
  • 216
  • 1
  • 3
  • Yeah those were just placeholders. I had it with a valid number. I tried it this way too and it just doesn't send the message. The message compose comes up and after hitting send it goes back down, but the message never goes through. Any ideas? – user3754489 Jul 04 '14 at 04:21
0

Try This In Your MessageCompose Delegate:

  • (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result

{

switch (result) {

    case MessageComposeResultCancelled:
        NSLog(@"Message was cancelled");
         break;
    case MessageComposeResultFailed:
        NSLog(@"Message failed");
          break;
    case MessageComposeResultSent:
        NSLog(@"Message was sent");
         break;
    default:             
        break;     
} 

[controller dismissViewControllerAnimated:YES completion:nil];

}

Kathiravan G
  • 487
  • 4
  • 11