1

我做了一个基本的应用程序,现在只有一个按钮,你可以点击它,它会调出短信编写器(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;     
    } 
}

编辑:所以我今天早上试了一下(我一夜之间什么也没做),它奏效了。不知道是什么问题。不过谢谢!

4

2 回答 2

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

[控制器dismissViewControllerAnimated:YES完成:无];

}

于 2014-07-04T07:46:20.017 回答
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];
于 2014-07-04T03:30:51.857 回答