Search code examples
iphonemfmailcomposeviewcontrollerpresentmodalviewcontrollernsexception

MFMailComposeViewController bringing up NSException


I have done a search for this problem, and have not found an answer, which is leading me to write my firs tquestion here.

The problem is when I press the button which calls the metho below, I get an exception on this line: [self presentModalViewController:mailViewController animated:YES]; and the exception is:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString count]: unrecognized selector sent to instance 0x20f6c'

In addition, I am running my app on the simulator if that helps.

I got the magority of the code from here.

Here is my code (with the email address blanked out):

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

What appears to be causing the exception?


Solution

  • In setToRecipients you have to specify an array, not a string. So you could pass your email-array.