如何使用UITableView发送电子邮件

时间:2014-04-30 05:44:31

标签: ios email uitableview send

所以我使用此数组在自定义表格单元格中显示电子邮件地址

 EmailAddress = [NSArray arrayWithObjects:@"sample@aths.ac.ae",:@"sample@aths.ac.ae",:@"sample@aths.ac.ae", :@"sample@aths.ac.ae",:@"sample@aths.ac.ae",:@"sample@aths.ac.ae", nil];

我将此代码用于我的电子邮件,但每当按下单元格时,我总会收到SIGABRT错误

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
    controller.mailComposeDelegate = self;
    NSArray *toRecipients = [EmailAddress objectAtIndex:indexPath.row];
    [controller setToRecipients:toRecipients];
    [controller setTitle:@""];
    [controller setSubject:@""];
    [controller setMessageBody:@"" isHTML:NO];

    if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
    {
        controller.modalPresentationStyle = UIModalPresentationFormSheet;
    }
    else
    {
        controller.modalPresentationStyle = UIModalPresentationFullScreen;
    }
    [self dismissViewControllerAnimated:YES completion:nil];

}


    - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
    [self becomeFirstResponder];
    NSString *strMailResult;
    switch (result)
    {
        case MFMailComposeResultCancelled:
            strMailResult = NSLocalizedString(@"E-Mail Cancelled",@"");
            break;
        case MFMailComposeResultSaved:
            strMailResult = NSLocalizedString(@"E-Mail Saved",@"");
            break;
        case MFMailComposeResultSent:
            strMailResult = NSLocalizedString(@"E-Mail Sent",@"");
            break;
        case MFMailComposeResultFailed:
            strMailResult = NSLocalizedString(@"E-Mail Failed",@"");
            break;
        default:
            strMailResult = NSLocalizedString(@"E-Mail Not Sent",@"");
            break;
    }

    UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Message",@"") message:strMailResult delegate:self  cancelButtonTitle:NSLocalizedString(@"OK",@"") otherButtonTitles:nil];
    [alertView show];
    [self dismissViewControllerAnimated:YES completion:nil];
}

但我最终得到错误我认为错误来自toRecipient,但我不知道如何解决它。

1 个答案:

答案 0 :(得分:3)

一个明显的问题是以下两行:

NSArray *toRecipients = [EmailAddress objectAtIndex:indexPath.row];
[controller setToRecipients:toRecipients];

应该是:

NSString *toRecipient = [EmailAddress objectAtIndex:indexPath.row];
[controller setToRecipients:@[ toRecipient ]];

因为您只从EmailAddress数组中获得了一个收件人值。