NSMutableArray的数据Attachement与电子邮件的身体?(NSMutableArr

2019-06-24 05:07发布

我NSMutableArray的数据在NSData的formate.I正在试图连接的NSMutableArray数据到电子邮件body.Here是我的NSMutableArray代码:

   NSUserDefaults *defaults1 = [NSUserDefaults standardUserDefaults];
   NSString *msg1 = [defaults1 objectForKey:@"key5"];
   NSData *colorData = [defaults1 objectForKey:@"key6"];
   UIColor *color = [NSKeyedUnarchiver unarchiveObjectWithData:colorData];
   NSData *colorData1 = [defaults1 objectForKey:@"key7"];
   UIColor *color1 = [NSKeyedUnarchiver unarchiveObjectWithData:colorData1];
   NSData *colorData2 = [defaults1 objectForKey:@"key8"];
   UIFont *color2 = [NSKeyedUnarchiver unarchiveObjectWithData:colorData2];
   CGFloat x =(arc4random()%100)+100;
   CGFloat y =(arc4random()%100)+250;  
   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];
   [viewArray addObject:lbl ];

viewArray是我的NSMutableArray。所有在viewArray数据存储在NSData的甲酸。那么如何才能连接这个viewArray数据与电子邮件body.here是我的电子邮件代码。

 - (IBAction)sendEmail
{

if ([MFMailComposeViewController canSendMail])
{
  NSArray *recipients = [NSArray arrayWithObject:@"example@yahoo.com"];
  MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] 
  init];
  controller.mailComposeDelegate = self;
  [controller setSubject:@"Iphone Game"];

   NSData *data = [NSKeyedArchiver archivedDataWithRootObject:viewArray];
   NSLog(@"testing: %@", data);
  [controller addAttachmentData:data mimeType:@"application/octet-stream";  
  fileName:nil]; 

  NSString *emailBody = @"Happy Valentine Day!";
  [controller setMessageBody:emailBody isHTML:NO
  [controller setToRecipients:recipients];
  [self presentModalViewController:controller animated:YES];
  [controller release];

  }
 else 
 {
  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert"
   message:@"Your device is not set up for email." 
                                           delegate:self 
                                  cancelButtonTitle:@"OK" 
                                  otherButtonTitles: nil];

 [alert show];

 [alert release];
}

 }

我没有得到任何错误.viewArray在这里展示这是店,并还当我转换viewArray到NSData的它显示console.but字节不显示在电子邮件body..which是viewArray.please任何一个导向的任何数据对象我怎么可能它连接到我的电子邮件viewArray数据。

Answer 1:

从MFMailComposeViewController参考的addAttachmentData:mimeType:fileName:

文件名

优选的文件名与所述数据相关联。 这是当它被转移到其目的地应用于该文件的默认名称。 在文件名中的任何路径分离器(/)字符被转换为现有下划线(_)字符来传输。 此参数不能是零。

这么看来,你必须指定要显示在邮件正文正确的文件名。 只要任何字符串就可以了。

编辑:

恐怕我不明白您的评论...正如我所说,我已经成功发送一封电子邮件,你的代码:我得到的是一个plist文件,所以一切都按预期工作。 这是我使用的代码:

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

路上,我会去,你的情况是:

  1. 忘了附着了一会儿,并尝试向您发送一个简单的文本电子邮件;

  2. 如果这样的作品,加上2线太发送附件:

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

在这两种情况下,你的委托方法设置断点- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error ,并看执行哪个分支:

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


文章来源: NSMutableArray Data Attachement With E-mail Body?