开发者

The correct way to dismiss a MFMailComposeViewController

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:UIApplic开发者_高级运维ationDidEnterBackgroundNotification 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];
}


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜