1

我正在深入研究 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 后尝试附加数据有关吗?

非常感谢你的智慧!

4

1 回答 1

4

你的怀疑是正确的。

引用文档

...呈现界面后,您的应用程序不允许对电子邮件内容进行进一步更改。用户仍然可以使用界面编辑内容,但程序更改会被忽略。因此,您必须在呈现界面之前设置内容字段的值。

于 2010-12-03T21:52:49.997 回答