2015-03-08 61 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

相關問題