Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Mail Composer Won't Dismiss

I am trying to take a screenshot and email it using the mail composer. Everything works great except the mail composer won't dismiss. This post seems to have the same problem, but the solution provided did not work for me. Can't dismiss the email composer view in iPhone?

- (IBAction)Email:(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] autorelease];
    mailComposer.delegate = self;
    [mailComposer setSubject:@"Risk Assessment"];
    [mailComposer addAttachmentData:imageData mimeType:@"image/jpeg" fileName:@"attachment.jpg"];     
    [self presentModalViewController:mailComposer animated:YES];        
}
}

The above code works great. How do I call this bottom portion. It seems like the compiler just skips past it.

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
if (error){
    NSString *errorTitle = @"Mail Error";
    NSString *errorDescription = [error localizedDescription];
    UIAlertView *errorView = [[UIAlertView alloc]initWithTitle:errorTitle message:errorDescription delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [errorView show];                
    [errorView release];
}
[controller dismissModalViewControllerAnimated:YES];

}

Thanks in advance.

like image 795
talbright Avatar asked Jul 11 '12 14:07

talbright


1 Answers

Try

mailComposer.mailComposeDelegate = self;

instead of

mailComposer.delegate = self;

From the MFMailComposeViewController documentation:

@property(nonatomic,assign) id<MFMailComposeViewControllerDelegate> mailComposeDelegate;

The delegate object is responsible for dismissing the view presented by this view controller at the appropriate time. Therefore, you should always provide a delegate and that object should implement the methods of the MFMailComposeViewControllerDelegate protocol.

like image 141
Bersaelor Avatar answered Sep 20 '22 00:09

Bersaelor