我正在深入研究iOS开发,并且正在熟悉MFMailComposeViewController类,用于发送带附件的电子邮件。我试图附加的数据是在运行时收集的信息,存储在NSDictionary中,并序列化为NSData,但每次发送电子邮件时,都没有附件的迹象。我的代码首先显示MFMailComposeViewController视图,其中已经填写了收件人电子邮件,正文和主题行。然后我会显示一个警告框,询问用户是否可以收集一些匿名数据。如果他们单击是,我的警报视图回调方法将编译数据并将其附加到MFMailComposeViewController。当我在调试器中单步调试时,所有数据都显示正确,但附加的数据永远不会随电子邮件一起到达。这是我的代码......
-(void)displayComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"Temp Subject Line"];
NSArray *toRecipients = [NSArray arrayWithObjects:@"support@tempaddress.com", nil];
[picker setToRecipients:toRecipients];
NSString *emailBody = @"Temporary email body";
[picker setMessageBody:emailBody isHTML:NO];
[self setMailViewController:picker];
[self presentModalViewController:picker animated:YES];
UIAlertView* uiav= [[UIAlertView alloc] initWithTitle: @"May we collect data from you?"
message: @"May we collect some data form you?"
delegate: self cancelButtonTitle: @"No" otherButtonTitles: nil];
[uiav addButtonWithTitle:@"Yes"];
[uiav setDelegate:self];
[uiav show];
[uiav release];
[picker release];
}
- (void) alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 1)
{
NSMutableDictionary *appData = [[[NSMutableDictionary alloc] init] autorelease];
.
. //Compile the application data to attach to email
.
NSString *errorString = [[[NSString alloc] init] autorelease];
NSData *attachData = [NSPropertyListSerialization dataFromPropertyList:appData format:NSPropertyListXMLFormat_v1_0 errorDescription:&errorString];
[[self mailViewController] addAttachmentData:attachData mimeType:@"text/xml" fileName:@"app data"];
}
}
}
有什么想法吗?是否与我在加载MFMailComposeViewController后尝试附加数据这一事实有关?
非常感谢你的智慧!
答案 0 :(得分:4)
你的怀疑是正确的。
Quoth the documentation
...在呈现界面后,你的 申请是不允许的 进一步更改电子邮件内容。用户仍然可以使用界面编辑内容,但忽略程序化更改。因此,您必须在呈现界面之前设置内容字段的值。