我的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; frame =(140 341; 56 19); text ='Xxxxxxx'; clipsToBounds = YES; layer =>但在电子邮件中我只看到这个..>>请建议任何一个如何在电子邮件中附加我的viewArray数据。 ]
答案 0 :(得分:1)
在电子邮件附件中,您只能将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