NSMutableArray Data Attachement With E-mail Body?

会有一股神秘感。 提交于 2019-11-29 08:58:47

From the MFMailComposeViewController reference for addAttachmentData:mimeType:fileName::

filename

The preferred filename to associate with the data. This is the default name applied to the file when it is transferred to its destination. Any path separator (/) characters in the filename are converted to underscore (_) characters prior to transmission. This parameter must not be nil.

So it seems you have to specify a proper filename to be displayed in the mail body. Just any string will do.

EDIT:

I am afraid I cannot understand your comment... as I said, I have successfully sent an email with your code: what I get is a plist file, so everything is working as expected. This is the code I am using:

NSUserDefaults *defaults1 = [NSUserDefaults standardUserDefaults];
NSString *msg1 = [defaults1 objectForKey:@"key5"];
UIColor *color = [UIColor grayColor];
UIColor *color1 = [UIColor grayColor];
UIFont *color2 = [UIFont systemFontOfSize:12];
CGFloat x =(arc4random()%100)+100;
CGFloat y =(arc4random()%100)+250;  
UILabel* lbl = [[UILabel alloc] initWithFrame:CGRectMake(x, y, 100, 70)];
lbl.userInteractionEnabled=YES;
lbl.text=msg1;
lbl.backgroundColor=color;
lbl.textColor=color1;
lbl.font =color2;
lbl.lineBreakMode = UILineBreakModeWordWrap;
lbl.numberOfLines = 50;
[self.view addSubview:lbl];
NSMutableArray* viewArray = [NSMutableArray arrayWithCapacity:1];
[viewArray addObject:lbl ];


if ([MFMailComposeViewController canSendMail]) {
    MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
    mailer.mailComposeDelegate = self;
    [mailer setSubject:@"Hello"];
    [mailer setToRecipients:[NSArray arrayWithObjects:@"mailAddress@mailAddress", nil]];
    NSString *emailBody = @"";
    [mailer setMessageBody:emailBody isHTML:NO];

    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:viewArray];
    [mailer addAttachmentData:data mimeType:@"application/octet-stream" fileName:@"prova.plist"]; 

    [self presentModalViewController:mailer animated:YES];
    [mailer release];
}

The way I would go, in your case is:

  1. forget about the attachment for a moment, and try to send you a simple text email;

  2. if that works, add the 2 lines that send the attachment too:

    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:viewArray];
    [mailer addAttachmentData:data mimeType:@"application/octet-stream" fileName:@"prova.plist"]; 
    

In both cases, set a breakpoint in your delegate method - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error and see which branch is executed:

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
switch (result) {
        case MFMailComposeResultCancelled:
        case MFMailComposeResultSaved:
        case MFMailComposeResultSent:
        case MFMailComposeResultFailed:
        default:
        break;
    }
[self dismissModalViewControllerAnimated:YES];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!