我有一个非常简单的MFMailComposeViewController实现。它运作良好 - 我从我的应用程序传递一个字符串,将通过电子邮件发送,然后触摸'发送',电子邮件将发送。没问题。
但是,如果我触摸“取消”按钮,应用程序将崩溃(EXC_BAD_ACCESS)。我是否必须实现取消MFMailComposeViewController的特殊方法?发送按钮将以正确的方式自动执行,但取消按钮不会。两者之间有什么区别(除了在一种情况下会发送电子邮件而在另一种情况下不会发送电子邮件)?
这是我的代码:
#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];
}
答案 0 :(得分:2)
试试这个:
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
NSString *message = @"";
// Notifies users about errors associated with the interface
switch (result) {
case MFMailComposeResultCancelled:
message = @"Mail: canceled";
break;
case MFMailComposeResultSaved:
message = @"Mail: saved";
break;
case MFMailComposeResultSent:
{
message = @"Mail: sent";
//Your code
}
break;
case MFMailComposeResultFailed:
message = @"Mail: failed";
break;
default:
message = @"Mail: not sent";
break;
}
[self dismissModalViewControllerAnimated:YES];
}