我试图使用FMailComposeViewController发送电子邮件。当我尝试通过调用presentModalViewController发送邮件时,应用程序崩溃了。在emulateR中,它会在设备上崩溃,它会在一半的时间内崩溃。
我没有收到错误消息,但app冻结,debuuger显示它会发生 int main(int argc,char * argv []) { @autoreleasepool { 返回UIApplicationMain(argc,argv,nil,NSStringFromClass([AppDelegate class])); } } 此外,应用程序总是在模拟器中崩溃,大约一半时间在iPhone上。
代码:
- (IBAction)aEmail:(id)sender {
if([MFMailComposeViewController canSendMail]){
MFMailComposeViewController *mailCtrl = [[MFMailComposeViewController alloc] init];
[mailCtrl setSubject:@"Your TellaFortune Card Reading"];
[mailCtrl setToRecipients:[NSArray arrayWithObject:@"tedpottel@yahoo.com"]];
mailCtrl.mailComposeDelegate = self;
[mailCtrl setMessageBody: @"hello" isHTML: false];
// CRASHES ON THID LINE
[self presentModalViewController:mailCtrl animated:NO];
// [mailCtrl release];
}
else
{
UIAlertView *alert=[[ UIAlertView alloc]
initWithTitle:@"Cannot send email"
message: @"Please check internet connection and email set up"
delegate: self
cancelButtonTitle:@"Ok"
otherButtonTitles: nil];
[alert show];
}
}
///////////////////////////////////////////////////////////////////////////////////////////
// if you do not have thid methed when sending emsil, app will freez after
// sent or cancel button has been pressed.
- (void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
[self dismissModalViewControllerAnimated:YES];
NSLog(@"Email result: %@", result==MFMailComposeResultCancelled?@"Cancelled":
result==MFMailComposeResultSaved?@"Saved":
result==MFMailComposeResultSent?@"Sent":
result==MFMailComposeResultFailed?@"Failed":@"Unknown");
}
答案 0 :(得分:1)
将mailctrl放在ivar中 - 你现在没有强烈的引用。