iphone - 电子邮件中附加数据?

标签 iphone ios nsmutablearray nsdata mfmailcomposeviewcontroller

我的 NSMutableArray 数据采用 NSData 格式。我正在尝试将 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 是我的 NSMutableArray 。viewArray 中的所有数据存储都采用 NSData 格式。我尝试使用此代码在电子邮件中附加 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"];
     NSLog(@"viewArray: %@", viewArray);
     NSString *string = [viewArray componentsJoinedByString:@"\n"];
     NSString *emailBody = string; 
     NSLog(@"test=%@",emailBody);
     [controller setMessageBody:emailBody isHTML:YES];
     [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.in 控制台中,它看起来像这样..[2012-05-07 18:48:00.065 Note List[279:207] test=UILabel: 0x8250850;帧=(140 341;56 19);文本 = 'Xxxxxxx';剪辑边界=是;层=>但在电子邮件中我只看到这个..>>请建议任何人如何在电子邮件中附加我的viewArray数据。 ]

最佳答案

在电子邮件附件中,您只能将 NSData 或字符串发送到电子邮件,现在如果您想通过字符串发送它,则获取您想要发送电子邮件的所有值,例如 lable.text、lable.color、lable.alpha 等,使用适当的键并将其放在正文中并在那里进行解析,否则找到某种方法将对象转换为 NSData 并使用 mfmailcomposer 附加数据方法附加它

读取此内容将 NSArray 转换为 NSData

How to convert NSArray to NSData?

这将 NSData 转换回 NSArray

How can i convert a NSData to NSArray?

然后将此数据写入文件,

-(void)writeDataToFile:(NSString*)filename
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    if(filename==nil)
    {
        DLog(@"FILE NAME IS NIL");
        return;
    }
    // the path to write file
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat: @"%@",filename]];
    /*NSData *writeData;
     writeData=[NSKeyedArchiver archivedDataWithRootObject:pArray]; */
    NSFileManager *fm=[NSFileManager defaultManager];
    if(!filePath)
    {
        //DLog(@"File %@ doesn't exist, so we create it", filePath);
        [fm createFileAtPath:filePath contents:self.mRespData attributes:nil];
    }
    else 
    {
        //DLog(@"file exists");
        [self.mRespData writeToFile:filePath atomically:YES];
    }

    NSMutableData *resData = [[NSMutableData alloc] init];
    self.mRespData=resData;
    [resData release];
}

最后使用将其附加到电子邮件

- (void)addAttachmentData:(NSData *)attachment mimeType:(NSString *)mimeType fileName:(NSString *)filename

关于iphone - 电子邮件中附加数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10483497/

相关文章:

ios - Google Maps SDK 在物理设备中返回 iOS 错误,但在模拟器中工作正常

iphone - 每次添加新设备进行临时安装时是否都必须重新编译?

iphone - 为什么我的对象没有被添加到数组中?

ios - 分析 iPad 项目的内存泄漏

objective-c - NSMutableArray - 强制数组只保存特定的对象类型

iphone - iPhone中分配的对象的潜在泄漏

iOS 10 问题 : UIScrollView Not Scrolling, 即使设置了 ContentSize

php - 保护 GET 请求的好方法?

ios - RestKit 映射了错误的对象

objective-c - ios 不允许使用 arc 将间接指针隐式转换为指向 id 的 objective-c 指针