带有电子邮件正文的 NSMutableArray 数据附件?
Posted
技术标签:
【中文标题】带有电子邮件正文的 NSMutableArray 数据附件?【英文标题】:NSMutableArray Data Attachement With E-mail Body? 【发布时间】:2012-05-07 08:37:10 【问题描述】:我的 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"];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:viewArray];
NSLog(@"testing: %@", data);
[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 转换为 NSData 时,它在控制台中显示字节。但不在电子邮件正文中显示任何数据..在 viewArray.please任何人都可以指导我如何使用电子邮件附加我的 viewArray 数据。
【问题讨论】:
【参考方案1】:从MFMailComposeViewController reference 到addAttachmentData:mimeType:fileName:
:
文件名
与数据关联的首选文件名。这是将文件传输到其目的地时应用到文件的默认名称。文件名中的任何路径分隔符 (/) 字符在传输之前都将转换为下划线 (_) 字符。此参数不能为 nil。
看来您必须指定一个正确的文件名才能显示在邮件正文中。任何字符串都可以。
编辑:
恐怕我无法理解您的评论...正如我所说,我已经成功发送了一封包含您的代码的电子邮件:我得到的是一个 plist 文件,所以一切都按预期工作。 这是我正在使用的代码:
NSUserDefaults *defaults1 = [NSUserDefaults standardUserDefaults];
NSString *msg1 = [defaults1 objectForKey:@"key5"];
UIColor *color = [UIColor grayColor];
UIColor *color1 = [UIColor grayColor];
UIFont *color2 = [UIFont systemFontOfSize:12];
CGFloat x =(arc4random()%100)+100;
CGFloat y =(arc4random()%100)+250;
UILabel* 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];
NSMutableArray* viewArray = [NSMutableArray arrayWithCapacity:1];
[viewArray addObject:lbl ];
if ([MFMailComposeViewController canSendMail])
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:@"Hello"];
[mailer setToRecipients:[NSArray arrayWithObjects:@"mailAddress@mailAddress", nil]];
NSString *emailBody = @"";
[mailer setMessageBody:emailBody isHTML:NO];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:viewArray];
[mailer addAttachmentData:data mimeType:@"application/octet-stream" fileName:@"prova.plist"];
[self presentModalViewController:mailer animated:YES];
[mailer release];
在你的情况下,我会走的路是:
暂时忘掉附件,试着给你发一封简单的短信;
如果可行,请添加发送附件的 2 行:
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:viewArray];
[mailer addAttachmentData:data mimeType:@"application/octet-stream" fileName:@"prova.plist"];
在这两种情况下,在您的委托方法- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
中设置一个断点,然后查看执行的是哪个分支:
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
switch (result)
case MFMailComposeResultCancelled:
case MFMailComposeResultSaved:
case MFMailComposeResultSent:
case MFMailComposeResultFailed:
default:
break;
[self dismissModalViewControllerAnimated:YES];
【讨论】:
Thanx @sergio.but 我没有在电子邮件中看到我的数组数据。 奇怪...我用正确的文件名尝试了代码,除了必须自己初始化 color/color1/color2(因为我没有设置用户默认值),它可以工作而且我可以在邮件正文中看到附件文件(不是数组数据,附件文件)...你希望看到什么?以上是关于带有电子邮件正文的 NSMutableArray 数据附件?的主要内容,如果未能解决你的问题,请参考以下文章