문제

I am trying to dismiss the mail from my app after it's done if user sends or cancels. But for some reasone this never dismisses. I tried almost everything. I have also logged this so I will see if it went to dissmiss method. And the problem is there since it never enters the dismiss method.

What am i doing wrong???

- (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.
}
도움이 되었습니까?

해결책

you could replace this line:

[self dismissModalViewControllerAnimated:YES];

with the following line:

[controller dismissModalViewControllerAnimated:YES];

다른 팁

MFMailComposeViewController class inherits from UINavigationController and so its delegate property is 'delegate' for navigation controller 'part' of the class. To handle specific mail composer delegate methods you need to set your object as mailComposeDelegate property:

mailComposer.mailComposeDelegate = self;

SWIFT 5.0:

If you implement the MFMailComposeViewControllerDelegate protocol, you only have to contain the following function in ViewController:

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

This function handles everything for you. If the user sends the email the view disappears automatically. Any further information: mailComposeController

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top