使用MFMessageComposeViewController发送消息失败

时间:2015-08-12 07:47:42

标签: ios objective-c sms mfmessagecomposeview imessage

我面临向多个联系人发送消息的一个问题:

301联系人:消息应用程序未打开,如果我们多次点击邀请,则iMessage正在打开,但加载消息需要更多时间(2分钟)

351 up联系人:消息已打开,黑色新消息屏幕然后返回我们的应用程序,打开消息屏幕。

这是我的代码: 联系人是电话号码

的数组
NSMutableArray *contacts = [NSMutableArray array];

for (User *user in users) {
    if (user.phone.length) {
        NSString *strphonenumber = [NSString stringWithFormat:@"%@",user.phone];
        [contacts addObject:strphonenumber];
    }
} 

MFMessageComposeViewController  *messanger = [[MFMessageComposeViewController alloc]init];
    messanger.messageComposeDelegate = self;
    messanger.recipients = contacts;
    messanger.body = [NSString stringWithFormat:@“body”;

    [self presentViewController:messanger animated:YES completion:NULL];

我收到此错误:

<CKSMSComposeRemoteViewController: 0x12802f810> timed out waiting for fence barrier from com.apple.mobilesms.compose

4 个答案:

答案 0 :(得分:0)

试试这个......

NSString *strContacts = [NSString stringWithFormat:@"%@",add multipal contacts];

MFMessageComposeViewController *message = [[MFMessageComposeViewController new];
message.recipients  = @[strContacts];

答案 1 :(得分:0)

我有同样的问题,然后想出

messanger.recipients = //应该始终是一个字符串数组。

确保您发送至messanger.recipients的电话号码为NSString

答案 2 :(得分:0)

这对我有用:

将委托设置为interface:

@interface ViewController <MFMessageComposeViewControllerDelegate>{}

检查设备是否能够发送消息

if([MFMessageComposeViewController canSendText] ){
  //device is possible to send messages
}else{
  //device can't send messages
}

准备消息:

MFMessageComposeViewController* comp = [[MFMessageComposeViewController alloc] init];

 //set properties
 comp.body = @"body";
 comp.recipients = [NSArray arrayWithObjects:phone1, phone2, nil];
 comp.messageComposeDelegate = self;

打开对话框:

[self presentViewController:comp animated:YES completion:nil];

确定结果

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
    //test result
    switch (result) {
        case MessageComposeResultCancelled:
            [self makeAlert:@"Result canceled"];
            break;
        //message was sent
        case MessageComposeResultSent:
            [self makeAlert:@"Result sent"];
            break;
        case MessageComposeResultFailed:
            [self makeAlert:@"Result Failed"];
            break;
        default:
            break;
    }

    //dismiss view
    [self dismissViewControllerAnimated:YES completion:nil];
}

答案 3 :(得分:0)

只需使用此方法:

- (IBAction)sendSMS:(id)sender {

   MFMessageComposeViewController *controller =  
   [[MFMessageComposeViewController alloc] init] autorelease];
   if([MFMessageComposeViewController canSendText]){
        controller.body = @"Hello";
        controller.recipients = [NSArray arrayWithObjects:@"12345678",@"87654321", nil];
        controller.messageComposeDelegate = self;
        [self presentViewController:controller animated:YES completion:nil];
    }
}

回调方法:

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
     switch (result) {
       case MessageComposeResultCancelled:
             NSLog(@"Cancelled");
             break;
        case MessageComposeResultFailed:
             NSLog(@"Error occured");
             break;
        case MessageComposeResultSent:
             break;
        default:
             break;
     }
    [self dismissViewControllerAnimated:YES completion:nil];
}