How can I Attached NSMutableArray Data With E-mail Body?

限于喜欢 提交于 2019-12-13 18:13:25

问题


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

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

}

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.


回答1:


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


来源:https://stackoverflow.com/questions/10454185/how-can-i-attached-nsmutablearray-data-with-e-mail-body

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!