未调用MFMailComposeViewController委托

时间:2015-08-14 07:46:29

标签: ios iphone mfmailcomposeviewcontroller

即使我在下面编写代码,我也无法调用Mail composer委托方法:

-(void)openMailComposerInViewController:(UIViewController *)hostController{
if ([MFMailComposeViewController canSendMail])
{
    // Email Subject
    NSString *emailTitle = @"Test Email";
    // Email Content
    NSString *messageBody = @"Test Email";
    // To address
    NSArray *toRecipents = [NSArray arrayWithObject:@"support@test.com"];

    mailComposer = [[MFMailComposeViewController alloc] init];
    mailComposer.mailComposeDelegate = self;
    [mailComposer setSubject:emailTitle];
    [mailComposer setMessageBody:messageBody isHTML:NO];
    [mailComposer setToRecipients:toRecipents];

    // Present mail view controller on screen

    [hostController presentViewController:mailComposer animated:YES completion:NULL];
    mailController = hostController;
}
else
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure"
                                                    message:@"Your device doesn't support the composer sheet"
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
}
}

pragma mark - Mail

- (void) mailComposeController:(MFMailComposeViewController *)controller    didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
  switch (result)
  {
    case MFMailComposeResultCancelled:
        NSLog(@"Mail cancelled");
        break;
    case MFMailComposeResultSaved:
        NSLog(@"Mail saved");
        break;
    case MFMailComposeResultSent:
        NSLog(@"Mail sent");
        break;
    case MFMailComposeResultFailed:
        NSLog(@"Mail sent failure: %@", [error localizedDescription]);
        break;
    default:
        break;
}

// Close the Mail Interface
[mailController dismissViewControllerAnimated:YES completion:NULL];
}

我不想在自己上调用邮件作曲家

但是,我的代表没有被调用。请帮忙。应用程序在单击“取消”或“发送邮件”按钮

时崩溃

3 个答案:

答案 0 :(得分:1)

我遇到了这个问题,我的问题是我有以下代码:

self.mailComposer.mailComposeDelegate = self
self.mailComposer = MFMailComposeViewController()

我在初始化mailComposer对象之前设置了委托。我刚刚将这两行换成了这个,它起作用了:

 self.mailComposer = MFMailComposeViewController()
 self.mailComposer.mailComposeDelegate = self

答案 1 :(得分:0)

由于ARC,我丢失了编写委托的viewcontroller,因此没有调用gettting。下面的代码帮助我解决了这个问题:

+(MailComposer *)sharedInstance
{

  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    sharedMailComposer = [[MailComposer alloc]init];
  });
  return sharedMailComposer;

}

然后我在调用MailComposerViewController

时使用了上面的实例
    [[MailComposer sharedInstance] openMailComposerInViewController:[(UINavigationController *)moduleRouter.container topViewController]];

工作:)

答案 2 :(得分:0)

对于setMailComposeDelegate和presentViewController,您的类实例必须相同。

all.equal()