iPhone不以编程方式发送文本

时间:2014-07-04 02:27:47

标签: ios mfmessagecomposeview

我制作了一个基本的应用程序,现在只需要一个单击的按钮,它会调出短信作曲家(ios7和xcode 5)。我想我已经很好地处理了一切。模拟器不支持发送消息,因此我尝试使用手机,但消息实际上并未发送。您可以单击发送按钮并取消按钮,但同样,消息永远不会发送。有任何想法吗?这是我的代码:

- (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;     
    } 
}
编辑:所以我今天早上尝试过(我一夜之间没有做任何事)并且有效。不确定是什么问题。谢谢!

2 个答案:

答案 0 :(得分:0)

我假设messageVC.recipients格式正确(数字数组)?

尝试:

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

答案 1 :(得分:0)

在MessageCompose代表中尝试此操作:

  • (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)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];

}