如何将NSMutableArray数据与电子邮件正文一起附加? [英] How can I Attached NSMutableArray Data With E-mail Body?

查看:57
本文介绍了如何将NSMutableArray数据与电子邮件正文一起附加?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将NSMutableArray数据附加到电子邮件正文.这是我的NSMutableArray代码:

I am trying to attached NSMutableArray data to E-mail body. Here is my NSMutableArray code:

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包含3个UILabel以及上面提到的所有这些属性,那么如何使用电子邮件正文将此viewArray附加.这是我的电子邮件代码.

Now if my viewArray Contain 3 UILabel along with all these properties which is mention above .Then how Can attached this viewArray With E-mail body.here is My E-mail Code.

   - (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];
   [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数据附加到电子邮件中.

I got NO Error .but not See any data in E-mail body..which is in viewArray.please Any One guide me how Can attached my viewArray data with Email.

推荐答案

在发送前检查NSData是否为空.

Check NSData is not nil before sending.

ie;

[controller setSubject:@"Iphone Game"];

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:viewArray];

if(!data){
    NSLog(@"Data is nil");

    return;
}
[controller addAttachmentData:data mimeType:@"application/octet-stream";  
fileName:nil]; 

这篇关于如何将NSMutableArray数据与电子邮件正文一起附加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆