关闭 MFMailComposeViewController 的正确方法

发布于 2024-12-06 18:17:07 字数 992 浏览 2 评论 0原文

我已经无计可施了。当我的应用程序转换到后台时,我需要关闭 MFMailComposeViewController ,但我无法做到这一点。它最终会创建一个尴尬的应用程序状态。

有没有办法以编程方式处理这个问题?也许强制视图控制器将电子邮件放入草稿文件夹并在没有动画的情况下关闭?

编辑:

-dismissModalViewControllerAnimated: 的调用无法按预期工作。

我所说的尴尬的应用程序状态是当应用程序从后台返回时,我的主视图在电子邮件编辑器之上重新绘制。该模式永远不会被关闭,并且该电子邮件编辑器永远无法再次访问。

编辑:

我的初始化程序中的代码:

// For compatibility with iOS versions below 4.0
if (&UIApplicationDidEnterBackgroundNotification != NULL)
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackgroundNotification:) name:UIApplicationDidEnterBackgroundNotification object:nil];
}

我的后台条目处理程序中的代码:

- (void) applicationDidEnterBackgroundNotification:(NSNotification *)note {

  // Do some other stuff here

  // According to the docs, calling the method like this closes all
  //  child views presented modally
  [self dismissModalViewControllerAnimated:NO];
}

I'm at my wit's end. I need to dismiss an MFMailComposeViewController when my app transitions to the background and I can't do it. It ends up creating an awkward application state.

Is there a way to handle this programmatically? Perhaps force the view controller to put the email into the Drafts folder and dismiss without animating?

EDIT:

Calls to - dismissModalViewControllerAnimated: don't work as expected.

The awkward application state I'm talking about is my main view being redrawn over top of the email composer when the application returns from the background. The modal is never dismissed and that email composer is never accessible again.

EDIT:

Code in my initializer:

// For compatibility with iOS versions below 4.0
if (&UIApplicationDidEnterBackgroundNotification != NULL)
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackgroundNotification:) name:UIApplicationDidEnterBackgroundNotification object:nil];
}

Code in my background-entry handler:

- (void) applicationDidEnterBackgroundNotification:(NSNotification *)note {

  // Do some other stuff here

  // According to the docs, calling the method like this closes all
  //  child views presented modally
  [self dismissModalViewControllerAnimated:NO];
}

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

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

发布评论

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

评论(1

樱娆 2024-12-13 18:17:07

我用上面的代码复制了一个简单的应用程序。当应用程序进入后台时,邮件编辑器将被正确关闭。

因此,我只能假设代码的 //Do some other stuff here 部分做了太多的事情,并且操作系统在您有机会解雇作曲家之前关闭了您。

根据 文档

在此方法退出之前,您应该执行与调整用户界面相关的任何任务,但其他任务(例如保存状态)应根据需要移至并发调度队列或辅助线程。因为您在 applicationDidEnterBackground: 中启动的任何后台任务可能只有在该方法退出后才会运行,因此您应该在启动这些任务之前请求额外的后台执行时间。换句话说,首先调用 beginBackgroundTaskWithExpirationHandler:,然后在调度队列或辅助线程上运行任务。

也许您应该将其他内容移至不同的线程或请求额外的时间?如果你删除其他东西,作曲家是否正确地驳回?

I have reproduced a simple application with the code you have above. The mail composer is dismissed properly when the application enters the background.

I can only assume therefore that the //Do some other stuff here section of your code is doing too much stuff and the OS is shutting you down before you have chance to dismiss the composer.

According to the docs:

You should perform any tasks relating to adjusting your user interface before this method exits but other tasks (such as saving state) should be moved to a concurrent dispatch queue or secondary thread as needed. Because it's likely any background tasks you start in applicationDidEnterBackground: will not run until after that method exits, you should request additional background execution time before starting those tasks. In other words, first call beginBackgroundTaskWithExpirationHandler: and then run the task on a dispatch queue or secondary thread.

Perhaps you should move your other stuff to a different thread or request extra time? If you remove the other stuff, does the composer dismiss correctly?

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