1

我在我的應用程序中實現了MFMailComposeViewController,當我點擊我的郵件按鈕時,我得到了下面的錯誤。應用程序試圖在iOS 10中的目標上呈現一個無模式視圖控制器?

應用程序試圖在目標強大的文本呈現零模態視圖控制器

這裏是我的代碼:

NSString *iOSVersion = [[UIDevice currentDevice] systemVersion]; 
    NSString * version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]; 

    NSString * build = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]; 

    mailComposer = [[MFMailComposeViewController alloc]init]; 
    mailComposer.navigationBar.tintColor = [UIColor blackColor]; 
    [mailComposer.navigationBar setTitleTextAttributes: 
    @{NSForegroundColorAttributeName:[UIColor blackColor]}]; 
    mailComposer.mailComposeDelegate = self; 

    [mailComposer setSubject:@"Co - Contact Us"]; 

    [mailComposer setToRecipients:@[@"[email protected]"]]; 
    NSString *supportText = @"Enter your comment here:\n\n\n\n\n"; 
    supportText = [supportText stringByAppendingString:[NSString stringWithFormat:@"iOS Version: %@\nCorner Version:%@\nBuild:%@\n\n",iOSVersion,version, build]]; 
    [mailComposer setMessageBody:supportText isHTML:NO]; 

    [self presentViewController:mailComposer animated:YES completion:nil]; 

這有什麼錯我的代碼。請幫忙

回答

2

如果設備沒有配置郵件或使用模擬器會導致崩潰或異常。

mailComposer = [[MFMailComposeViewController alloc] init]; 

上面的代碼行可能會導致問題。在模擬器初始化方法中可能會返回NULLnil

,只需檢查canSendMail呈現模式的viewController前:

一樣,

NSString *iOSVersion = [[UIDevice currentDevice] systemVersion]; 
    NSString * version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]; 

    NSString * build = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]; 

    mailComposer = [[MFMailComposeViewController alloc]init]; 

    if ([MFMailComposeViewController canSendMail] && mailComposer) { 

     mailComposer.navigationBar.tintColor = [UIColor blackColor]; 
     [mailComposer.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}]; 
     mailComposer.mailComposeDelegate = self; 

     [mailComposer setSubject:@"Corner - Contact Us"]; 

     [mailComposer setToRecipients:@[@"[email protected]"]]; 
     NSString *supportText = @"Enter your comment here:\n\n\n\n\n"; 
     supportText = [supportText stringByAppendingString:[NSString stringWithFormat:@"iOS Version: %@\nCorner Version:%@\nBuild:%@\n\n",iOSVersion,version, build]]; 
     [mailComposer setMessageBody:supportText isHTML:NO]; 

     [self presentViewController:mailComposer animated:YES completion:nil]; 
} 
+0

爲MFMailcomposerViewController @visible接口聲明的選擇 'canSendMail' 我收到此錯誤 –

+0

嘗試編輯答案,有錯別字錯誤@ User558 –

+0

我試過了,但它沒有進入IF循環 –

相關問題