MFMailComposeViewController调出NSException

时间:2011-04-05 10:12:09

标签: iphone mfmailcomposeviewcontroller presentmodalviewcontroller nsexception

我已经找到了这个问题,并没有找到答案,这导致我在这里写下我的第一个问题。

问题是,当我按下调用下面的方法的按钮时,我在这一行上得到一个例外:[self presentModalViewController:mailViewController animated:YES];,例外是:

  

* 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [NSCFString count]:无法识别的选择器发送到实例0x20f6c'

此外,如果有帮助,我正在模拟器上运行我的应用程序。

我从here获得了代码的重要性。

这是我的代码(电子邮件地址已被删除):

    // set up the email address array
    NSArray *email = [[NSArray alloc] initWithObjects:@"foo", nil];

    // Set up the view controller
    MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
    mailViewController.mailComposeDelegate = self;

    // Set up the email to send to
    [mailViewController setSubject:@"foo"];
    [mailViewController setToRecipients:[email objectAtIndex:0]];

    // Get the path to the plist
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsPath = [paths objectAtIndex:0];  
    NSString *path = [documentsPath stringByAppendingPathComponent:@"foo.plist"];

    // Get the plist from the path
    NSData *myData = [NSData dataWithContentsOfFile:path];
    [mailViewController addAttachmentData:myData mimeType:@"plist" fileName:@"foo"];

    // Fill out the email body text 
    NSString *emailBody = @"Attached PLIST file";
    [mailViewController setMessageBody:emailBody isHTML:NO];

    // Release the array
    [email release];

    if (mailViewController != nil) {
        [self presentModalViewController:mailViewController animated:YES];
        [mailViewController release];
    }

什么似乎导致了例外?

2 个答案:

答案 0 :(得分:2)

setToRecipients中,您必须指定一个数组,而不是字符串。所以你可以传递你的email - 数组。

答案 1 :(得分:1)

-(IBAction)btnemail:(id)sender{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;

[picker setSubject:@"subject"];
NSString *emailid=btnEmail.titleLabel.text;
NSArray *toRecipients = [NSArray arrayWithObject:emailid];
[picker setToRecipients:toRecipients];

NSString *emailBody = @"";
[picker setMessageBody:emailBody isHTML:NO];

if ([MFMailComposeViewController canSendMail]) {
    [self.parentViewController presentModalViewController:picker animated:YES];
}else {

}
[picker release];

}