【问题标题】:How can I Attached NSMutableArray Data With E-mail Body?如何使用电子邮件正文附加 NSMutableArray 数据?
【发布时间】:2012-05-04 18:24:25
【问题描述】:

我正在尝试将 NSMutableArray 数据附加到电子邮件正文。这是我的 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 包含 3 个 UILabel 以及上面提到的所有这些属性。那么如何将这个 viewArray 附加到电子邮件正文中。这是我的电子邮件代码。

   - (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 数据附加到电子邮件中。

【问题讨论】:

    标签: iphone objective-c xcode nsmutablearray mfmailcomposeviewcontroller


    【解决方案1】:

    发送前检查 NSData 是否为 nil。

    即;

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

    【讨论】:

    • 感谢您的回复。 NSData 在我使用断点时显示字节。但我无法理解为什么我的 viewArray 数据没有显示在电子邮件中。请亲爱的指导我该怎么做。
    猜你喜欢
    • 2012-05-17
    • 1970-01-01
    • 2019-12-31
    • 2012-10-02
    • 2011-04-29
    • 2011-05-07
    • 2020-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多