从 NSObject 调用时,邮件撰写视图控制器不会关闭

Mail Compose View Controller won't dismiss when called from NSObject

我有几个视图控制器需要发送电子邮件,所以为了保持面向对象,我创建了一个名为 MessagingObject 的 NSObject class 来处理这些消息。但我不确定如何关闭 MailComposeVC,因为它来自非 VC 对象。实现如下所示:

//.m file
#import "MessagingObject.h"
#import <MessageUI/MessageUI.h>

@interface MessagingObject () <MFMessageComposeViewControllerDelegate, MFMailComposeViewControllerDelegate>

@end
@implementation MessagingObject


.....

#pragma mark - Email Messaging Methods

- (void)sendEmail:(NSString *)description withSubject:(NSString *)subj toRecipients:(NSArray *) recipients fromController:(id)sender{

    // Building the email content

     MFMailComposeViewController *mc = [[MFMailComposeViewController     alloc] init];
     mc.mailComposeDelegate = sender; //Delegated to the sending VC so it could bring out the composer
    [mc setSubject:subj];
    [mc setMessageBody:description isHTML:YES];
    [mc setToRecipients:recipients];


    // Present mail view controller on screen from the sender VC
    [sender presentViewController:mc animated:YES completion:NULL];
}


- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
    //Check the result of the email being sent
    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 here, this won't work with *self* (which references the MessagingObject)
    //[self dismissViewControllerAnimated:YES completion:NULL];

    //This delegate will notify the corresponding VC when the mail has been sent
    if (self.delegate && [self.delegate respondsToSelector:@selector(messageDidSend)]) {
        [self.delegate messageDidSend];
    }
}
@end

这是用于调用 MessagingObject 的方法:

- (IBAction)sendEmail:(id)sender {

    MessagingObject *aMessenger = [[MessagingObject alloc] init];
    aMessenger.delegate = self;

    NSString desc = @"A sample Description";        
    NSString subj = @"A sample subject";

    NSArray *recipients = [NSArray arrayWithObjects:@"recipient@gmail.com", nil];
    [aMessenger sendMail:desc withSubject:subj recipientList:recipients fromController:self];
}

我尝试在我的头文件中使用一个名为 MessengerDelegate 的委托,使用方法 messageDidSend: 来通知 VC关闭 MailComposeVC。但出于某种原因,mailComposeController didFinishWithResult 没有被调用,我认为这与我如何委托事情有关。无论如何,我怎么能关闭 MailComposeVC?

您有两个问题:

  1. mailComposeDelegate 设置为 self,而不是 sender,因为 self 实现了委托方法。

    mc.mailComposeDelegate = self;
    
  2. 在委托方法中的 controller 上调用 dismiss...

    [controller dismissViewControllerAnimated:YES completion:NULL];