在应用程序中 - MFMailComposeViewController-错误消息



我的代码如下:

viewController.m:

#import "ViewController.h"
#import "MyScene.h"
#import "MainMenu.h"
#import <MessageUI/MessageUI.h>
@implementation ViewController
- (IBAction)showMailPicker:(id)sender
{
 // You must check that the current device can send email messages before you
 // attempt to create an instance of MFMailComposeViewController.  If the
 // device can not send email messages,
 // [[MFMailComposeViewController alloc] init] will return nil.  Your app
 // will crash when it calls -presentViewController:animated:completion: with
 // a nil view controller.
 if ([MFMailComposeViewController canSendMail])
      // The device can send email.
 {
      [self displayMailComposerSheet];
 }
 else
      // The device can not send email.
 {
      self.feedbackMsg.hidden = NO;
      self.feedbackMsg.text = @"Device not configured to send mail.";
 }
}

- (IBAction)showSMSPicker:(id)sender
{
 // You must check that the current device can send SMS messages before you
 // attempt to create an instance of MFMessageComposeViewController.  If the
 // device can not send SMS messages,
 // [[MFMessageComposeViewController alloc] init] will return nil.  Your app
 // will crash when it calls -presentViewController:animated:completion: with
 // a nil view controller.
 if ([MFMessageComposeViewController canSendText])
      // The device can send email.
 {
      [self displaySMSComposerSheet];
 }
 else
      // The device can not send SMS.
 {
      self.feedbackMsg.hidden = NO;
      self.feedbackMsg.text = @"Device not configured to send SMS.";
 }
}

- (void)displayMailComposerSheet
{
 MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
 picker.mailComposeDelegate = self;
 [picker setSubject:@"Support for Brick Smasher Extreme"];
 // Set up recipients
 NSArray *toRecipients = [NSArray arrayWithObject:@"help.bricksx@gmail.com"];
 //NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com",     @"third@example.com", nil];
 //NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com"];
 [picker setToRecipients:toRecipients];
 //[picker setCcRecipients:ccRecipients];
 //[picker setBccRecipients:bccRecipients];
 // Attach an image to the email
 //NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"jpg"];
 //NSData *myData = [NSData dataWithContentsOfFile:path];
 //[picker addAttachmentData:myData mimeType:@"image/jpeg" fileName:@"rainy"];
 // Fill out the email body text
 NSString *emailBody = @"Message:";
 [picker setMessageBody:emailBody isHTML:YES];
 //[self presentViewController:picker animated:YES completion:NULL];
 [[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController:picker
                                                                                          animated:YES
                                                                              completion:nil];
}

- (void)mailComposeController:(MFMailComposeViewController *)controller
      didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
 self.feedbackMsg.hidden = NO;
 // Notifies users about errors associated with the interface
 switch (result)
 {
      case MFMailComposeResultCancelled:
           self.feedbackMsg.text = @"Result: Mail sending canceled";
           break;
      case MFMailComposeResultSaved:
           self.feedbackMsg.text = @"Result: Mail saved";
           break;
      case MFMailComposeResultSent:
           self.feedbackMsg.text = @"Result: Mail sent";
           break;
      case MFMailComposeResultFailed:
           self.feedbackMsg.text = @"Result: Mail sending failed";
           break;
      default:
           self.feedbackMsg.text = @"Result: Mail not sent";
           break;
 }
 [self dismissViewControllerAnimated:YES completion:NULL];
}
@end

错误是:Warning: Attempt to present <MFMailComposeViewController: 0xbf95170> on <UIViewController: 0xbf78d10> whose view is not in the window hierarchy!

我正在尝试通过我的应用发送电子邮件。使用MFMailComposeViewController,它始终给出一个错误(上图)。但是当我使用SMS部分时,根本没有问题。

使用:Xcode 5,iOS 7,iPhone Retina 4"

您的问题可能在这里: [[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController:

首先,您已经从ViewController调用了此功能,因此您只能使用[self presentViewController:]。听起来确实可能是您在可见ViewController的视图之前调用displayMailComposerSheetviewDidLoadviewWillAppear),这会增加您看到的错误。

使用

[self presentViewController:picker animated:YES completion:nil];

而不是

[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController:picker                                                                             animated:YES                                                                              completion:nil];

相关内容

最新更新