2015-03-08 65 views
0

我试图使用FMailComposeViewController发送电子邮件。当我尝试通过调用presentModalViewController发送邮件时,应用程序崩溃。在仿真R中,它在设备上的所有时间都会崩溃,只有一半时间会崩溃。试图发送邮件和应用程序崩溃

我没有得到一个错误消息,但应用程序冻结和debuuger表明它打算 INT主(INT ARGC,CHAR *的argv []){ @autoreleasepool { 回报UIApplicationMain(ARGC,ARGV,零, 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:@"[email protected]"]]; 
    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: %@", [email protected]"Cancelled": 
     [email protected]"Saved": 
     [email protected]"Sent": 
     [email protected]"Failed":@"Unknown"); 
} 
+0

请更新您在控制台中看到的错误消息。 – 2015-03-08 17:15:39

+0

你在模拟器上测试吗?我已经看到它在模拟器上不可思议。在真实的设备上进行测试。也可以尝试使用'[self presentViewController]' – soulshined 2015-03-08 19:46:34

回答

1

把mailctrl在伊娃 - 你没有很强的参考现在。

+0

不需要对正在呈现的控制器进行强有力的引用。 – 2015-03-08 17:17:29

+0

嗨,你能解释一下你的意思吗?谢谢 – 2015-03-09 13:51:28

+0

你正在使用方法中的局部变量创建对象。难道你不知道什么是目标吗? - 一个实例变量。您将使'MFMailComposeViewController * mailCtrl'成为一个实例变量,将其设置为现在的位置,并且在委托方法完成其工作后将其设置为零(将其设置为零)。 – 2015-03-09 20:33:11

相关问题