iOS 开发:为什么我无法将 plist 数据附加到 MFMailComposeViewController?

发布于 2024-10-05 18:38:08 字数 2249 浏览 6 评论 0原文

我正在深入研究 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:@"[email protected]", 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 之后尝试附加数据的事实有什么关系吗?

非常感谢您的智慧!

I'm diving into iOS development and am playing getting familiar with the MFMailComposeViewController class for sending emails with attachments. The data I'm trying to attach is information collected at run time, stored in an NSDictionary, and serialized to NSData, but every time the email is sent, there's no sign of an attachment. My code first display the MFMailComposeViewController view with the recipient email, body, and subject lines already filled in. Then I display an alert box to ask the user if I can collect some anonymous data. If they click yes, my alert view callback method compiles the data and attaches it to the MFMailComposeViewController. All the data appears correct when I step through it in the debugger, but the attached data never arrives with the email. Here's my code...

-(void)displayComposerSheet
{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;
    [picker setSubject:@"Temp Subject Line"];

      NSArray *toRecipients = [NSArray arrayWithObjects:@"[email protected]", 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"]; 
        }   
    }
}

Any ideas? Does it have anything to do with the fact that I'm trying to attach the data AFTER I load the MFMailComposeViewController?

Thanks so much for your wisdom!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬﹪忆之殇 2024-10-12 18:38:08

你的怀疑是正确的。

引用文档

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

Your suspicion is correct.

Quoth the documentation

...after presenting the interface, your
application is not allowed to make
further changes to the email content. The user may still edit the content using the interface, but programmatic changes are ignored. Thus, you must set the values of content fields before presenting the interface.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文