I have a very simple implementation of MFMailComposeViewController. It works very well – I pass a string from my app which will be e-mailed and then touch ‘send’ and the e-mail will send. No problems.
However, if I touch the ‘cancel’ button, the app will crash (EXC_BAD_ACCESS). Do I have to implement a special method for the cancellation of a MFMailComposeViewController? The send button will automatically do it the right way, but the cancel button won’t. What is the difference between the two (except that in one case the e-mail will be sent and in the other it won’t)?
Here is my code:
#pragma mark - EMail
-(IBAction)emailCurrentPage:(id)sender {
NSString *textToBeSend = @"Test";
MFMailComposeViewController *mailComposer;
mailComposer=[[MFMailComposeViewController alloc] init];
mailComposer.mailComposeDelegate=self;
[mailComposer setMessageBody:textToBeSend isHTML:NO];
[self presentModalViewController:mailComposer animated:YES];
[mailComposer release];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
if (error) {
NSString *msgOFF = [[NSString alloc] initWithFormat:@"I could not send the e-mail for the following reason: %@", error];
UIAlertView *alertOFF = [[UIAlertView alloc]
initWithTitle:@"Error"
message:msgOFF
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertOFF show];
[alertOFF release];
[msgOFF release];
}
[self dismissModalViewControllerAnimated:NO];
}
Try this :