NSMutableArray Data Attachement With E-mail Body?

前端 未结 1 1573
失恋的感觉
失恋的感觉 2020-12-18 17:34

My NSMutableArray data are in NSData formate.I am trying to attached NSMutableArray data to E-mail body.Here is my NSMutableArray code:

   NSUserDefaults *de         


        
相关标签:
1条回答
  • 2020-12-18 17:48

    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];
    }
    
    0 讨论(0)
提交回复
热议问题