talbright
talbright

Reputation: 446

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.

Upvotes: 6

Views: 4587

Answers (2)

Dean Davids
Dean Davids

Reputation: 4214

I am pretty sure that last line should be

[self dismissModalViewControllerAnimated:YES];

The ViewController that presented the view modally, also dismisses it.

Upvotes: 2

Bersaelor
Bersaelor

Reputation: 2577

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.

Upvotes: 17

Related Questions