电子邮件正文的NSMutableArray数据附加? [英] NSMutableArray Data Attachement With E-mail Body?
问题描述
我的NSMutableArray数据在NSData格式中。我正在尝试将NSMutableArray数据附加到电子邮件正文。这是我的NSMutableArray代码:
My NSMutableArray data are in NSData formate.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 ];
viewArray是我的NSMutableArray。viewArray中的所有数据存储都是NSData格式。然后如何附加这个viewArray data with E-mail body.here是我的电子邮件代码。
viewArray is my NSMutableArray .All the data store in viewArray are in NSData formate .Then how Can attached this viewArray data 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];
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 show这里存储在其中的对象,当我将viewArray转换为NSData时,它在控制台中显示字节。但是不显示电子邮件正文中的任何数据..这是在viewArray.please任何一个指导我如何附加我的viewArray data with Email。
I get NO Error .viewArray show here the objects which is store in it and also when i convert viewArray to NSData it show bytes in console.but not Show any data in E-mail body..which is in viewArray.please Any One guide me how its possible to attached my viewArray data with Email.
推荐答案
来自为> addAttachmentData:mime类型:文件名:
:
From the MFMailComposeViewController reference for addAttachmentData:mimeType:fileName:
:
filename
filename
与数据关联的首选文件名。这是传输到目标时应用于文件的默认名称。文件名中的任何路径分隔符(/)字符在传输之前都将转换为下划线(_)字符。此参数不能为零。
The preferred filename to associate with the data. This is the default name applied to the file when it is transferred to its destination. Any path separator (/) characters in the filename are converted to underscore (_) characters prior to transmission. This parameter must not be nil.
因此,您似乎必须指定要在邮件正文中显示的正确文件名。任何字符串都可以。
So it seems you have to specify a proper filename to be displayed in the mail body. Just any string will do.
编辑:
我恐怕无法理解你的评论......我说,我已经成功发送了一封包含您的代码的电子邮件:我得到的是一个plist文件,所以一切都按预期工作。
这是我正在使用的代码:
I am afraid I cannot understand your comment... as I said, I have successfully sent an email with your code: what I get is a plist file, so everything is working as expected. This is the code I am using:
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];
}
在我看来,我的方式是:
The way I would go, in your case is:
-
暂时忘记附件,并尝试向您发送简单的短信;
forget about the attachment for a moment, and try to send you a simple text email;
如果可行,请添加发送附件的2行:
if that works, add the 2 lines that send the attachment too:
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:viewArray];
[mailer addAttachmentData:data mimeType:@"application/octet-stream" fileName:@"prova.plist"];
在这两种情况下,请在您的设置中设置断点委托方法 - (无效)mailComposeController:(MFMailComposeViewController *)控制器didFinishWithResult:(MFMailComposeResult)结果错误:(NSError *)错误
并查看执行哪个分支:
In both cases, set a breakpoint in your delegate method - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
and see which branch is executed:
- (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];
}
这篇关于电子邮件正文的NSMutableArray数据附加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!