目前,我正在尝试通过应用程序从用户手机发送短信。
想法是,用户选择邮件的收件人,然后将邮件一一发送给每个收件人。
目前,这是我在单击按钮后所做的事情

if (self.abNumbersSelected.count > 0){
                for(NSString *phoneNumber in self.abNumbersSelected)
                {
                    if(self.currentController)
                        [self.currentController dismissViewControllerAnimated:YES completion:^{[self sendInviteMessage:phoneNumber];}
                         ];
                    else
                        [self sendInviteMessage:phoneNumber];// message that invites selected message
                }
            }
abNumbersSelected是用户选择了sendInviteMessage的数字数组,位于:
- (void)sendInviteMessage:(NSString*)invitingNumber
{
    //Invites the numbers using sms
    //First checks if it duplicates it
    if ([self checkDuplicate:invitingNumber]) {
        UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Invalid Phone Number" message:@"The phone number you entered is not valid." delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
        [alertView show];
        self.phoneField.text = @"";
    }
    //if not a duplicate then it sends the message
    else {
        if (SharedSessionManager.authToken) {
            NSString* currentUserName = @"";
            if (SharedSessionManager.currentUser.firstName && SharedSessionManager.currentUser.lastName) {
                currentUserName = [NSString stringWithFormat:@"%@ %@", SharedSessionManager.currentUser.firstName, SharedSessionManager.currentUser.lastName];
            }
            else if (SharedSessionManager.currentUser.firstName) {
                currentUserName = SharedSessionManager.currentUser.firstName;
            }
            if (!customLoadingView.isLoading)
                [customLoadingView beginLoading];
            MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
            if([MFMessageComposeViewController canSendText])
            {
                NSString *message = [NSString stringWithFormat:@"%@ invited you to join Friendli - a mobile, location based social network. Join now at friendli.co",currentUserName];
                controller.body = message;
                controller.recipients = [NSArray arrayWithObjects:invitingNumber, nil];
                controller.messageComposeDelegate = self;
                controller.disableUserAttachments;
                controller.title = @"Invite Friend";
                self.currentController = controller;
                [self presentModalViewController:self.currentController animated:YES];
            }
        }
    }

}

当前没有消息显示要发送。
这也是messageComposeViewController:方法
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult) result
{
    switch (result) {
        case MessageComposeResultCancelled:
            break;

        case MessageComposeResultFailed:
        {
            UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to send SMS!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [warningAlert show];
            break;
        }

        case MessageComposeResultSent:
            break;

        default:
            break;
    }

    [self dismissViewControllerAnimated:YES completion:nil];
}

在日志中,这就是说当选择2个收件人并且用户单击触发创建MFMessageComposeViewController的按钮时的意思。

2015-06-05 13:29:10:521 Friendli [241:807] [self.abNumbersSelected计数]:2

2015-06-05 13:29:11:638 Friendli [241:807]从钥匙串读取身份验证 token 。 (friendli.116.authtoken)

2015-06-05 13:29:11:643 Friendli [241:807]从钥匙串读取身份验证 token 。 (friendli.116.authtoken)

2015-06-05 13:29:12.869 Friendli [241:10525]远程撰写控制器超时(否)!

最佳答案

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult) result
{
    switch (result) {
        case MessageComposeResultCancelled:
            break;

        case MessageComposeResultFailed:
        {
            UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to send SMS!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [warningAlert show];
            break;
        }

        case MessageComposeResultSent:
            break;

        default:
            break;
    }
    [self dismissViewControllerAnimated:YES completion:^{
        if (self.abNumbersSelected.count > 0 && self.multipleRecipients) {
            [self sendInviteMessage:[self.abNumbersSelected objectAtIndex:0]];
        }
    }];
}

在dismiss语句中的MFMessageComposeController的didFinish方法中,使用代码块中的if语句,直到完成,代码将继续创建更多MFMessageComposeViewControllers。

10-08 15:37