关闭mailComposeController

时间:2011-11-01 18:41:07

标签: objective-c mfmailcomposer

如果用户发送或取消,我试图在我的应用程序完成后关闭邮件。 但对某些人来说,这绝不会让人失望。我几乎尝试了一切。 我也记录了这个,所以我会看看它是否去了dissmiss方法。问题出现了,因为它从未进入解雇方法。

我做错了什么???

- (IBAction)sendmail:(id)sender{
    UIGraphicsBeginImageContext(self.view.frame.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    NSData * imageData = UIImageJPEGRepresentation(image, 1.0);

    if ( [MFMailComposeViewController canSendMail] ) {
        MFMailComposeViewController * mailComposer = [[MFMailComposeViewController alloc] init];
        mailComposer.delegate = self;
        [mailComposer addAttachmentData:imageData mimeType:@"image/jpeg" fileName:@"attachment.jpg"];

        [mailComposer setSubject:@"Hello from My App!"];

        NSString *emailBody = @"Sent from My App, Still not in AppStore!";
        [mailComposer setMessageBody:emailBody isHTML:YES];

        [self presentModalViewController:mailComposer animated:YES];
    }
}

-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{ 
    [self dismissModalViewControllerAnimated:YES];

    NSLog (@"mail finished"); // NEVER REACHES THIS POINT.
}

3 个答案:

答案 0 :(得分:4)

你可以替换这一行:

[self dismissModalViewControllerAnimated:YES];

使用以下行:

[controller dismissModalViewControllerAnimated:YES];

答案 1 :(得分:3)

MFMailComposeViewController类继承自UINavigationController,因此其委托属性为类的导航控制器“部分”的“委托”。要处理特定的邮件编辑器委托方法,您需要将对象设置为mailComposeDelegate属性:

mailComposer.mailComposeDelegate = self;

答案 2 :(得分:0)

SWIFT 5.0:

如果实现MFMailComposeViewControllerDelegate协议,则只需在ViewController中包含以下功能:

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) 
{
   controller.dismiss(animated: true, completion: nil)
}

此功能为您处理所有事情。如果用户发送电子邮件,则视图自动消失。 任何进一步的信息: mailComposeController