从NSObject调用时,Mail Compose View Controller不会被忽略

时间:2015-05-20 18:47:08

标签: objective-c cocoa-touch

我有几个需要发送电子邮件的View Controller,所以为了保持面向对象,我创建了一个名为 MessagingObject 的NSObject类来处理这些消息。但我不知道如何解散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何时解除Ma​​ilComposeVC。但由于某种原因, mailComposeController didFinishWithResult 没有被调用,我认为它与我委托的事情有关。无论如何,我怎么能解雇MailComposeVC?

1 个答案:

答案 0 :(得分:2)

您有两个问题:

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

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

    [controller dismissViewControllerAnimated:YES completion:NULL];