user3754489
user3754489

Reputation: 41

iPhone not sending text programmatically

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!

Upvotes: 1

Views: 149

Answers (2)

Kathiravan G
Kathiravan G

Reputation: 487

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

}

Upvotes: 0

joelrb
joelrb

Reputation: 216

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

Upvotes: 0

Related Questions