使用MFMessageComposeViewController单独发送到数字数组

时间:2015-06-05 17:52:44

标签: ios sms mfmessagecomposeview

目前我正在尝试通过应用从用户手机发送短信。 想法是用户选择消息的接收者,然后逐个地将消息发送给每个接收者。 目前这是我点击按钮后发生这种情况的方式

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 count]:2

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

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

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

1 个答案:

答案 0 :(得分:1)

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

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