在发送按钮之前单击联系人按钮时出现黑屏

Posted

技术标签:

【中文标题】在发送按钮之前单击联系人按钮时出现黑屏【英文标题】:Getting a black screen when clicking the contact button prior to the send button 【发布时间】:2018-05-13 04:12:57 【问题描述】:

只是尝试允许用户发送电子邮件。在单击“联系人”按钮的那一刻,它会将我带到黑屏而不是显示 mailComposers。

调试器响应

2018-05-14 11:10:59.465952-0400 App[2333:757177] 无法在 (UIButton) 上设置 (keyPath) 用户定义的检查属性:[setValue:forUndefinedKey:]:此类不是键值编码- 符合键 keyPath。

但是,这只发生在使用 SWReveal 函数从左向右滑动菜单时。从下面删除代码时,所有其他功能都可以正常工作。只有在使用下面的代码时,才会在按下“按钮接触”时给我黑屏。

import Foundation
import UIKit
import MessageUI

class SendEmailVC: MFMailComposeViewController, MFMailComposeViewControllerDelegate

@IBAction func Send_Tapped(_ sender: Any)


    if MFMailComposeViewController.canSendMail()
    
        contact()
        let mailComposeViewController = configureMailController() //FIXED √
        self.present(mailComposeViewController, animated: true, completion: nil)
    
    else
    
        showMailError()
    


func configureMailController() -> MFMailComposeViewController

    let mailComposerVC = MFMailComposeViewController()
    mailComposerVC.mailComposeDelegate = self

    mailComposerVC.setToRecipients(["testing@gmail.com"])
    mailComposerVC.setSubject("Hello")
    mailComposerVC.setMessageBody("How are you doing?", ishtml: false)

    return mailComposerVC



/*
 * DON'T EDIT THE CODE BELOW.
 */


func showMailError()

    let sendMailErrorAlert = UIAlertController(title: "Email failed to send", message: "Your device fail to send the email", preferredStyle: .alert)

    let dismiss = UIAlertAction(title: "Dale", style: .default, handler: nil)
    sendMailErrorAlert.addAction(dismiss)
    self.present(sendMailErrorAlert, animated: true, completion: nil)


func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?)

    controller.dismiss(animated: true, completion: nil)

【问题讨论】:

仅供参考 - 您需要在检查 canSendMail 后致电 configureMailController 运行此代码时,调试器控制台中是否有任何消息?您是在真实的 ios 设备上进行测试吗? rmaddy,我到底应该在哪里调用 configureMailController,我知道你在 canSendMail 之后说过,我收到警告说“调用结果 _ 未使用”。我得到的调试器中唯一的消息是 2018-05-13 00:09:04.648632-0400 TestingApp [1015:294288] 无法在(UIButton)上设置(keyPath)用户定义的检查属性:[ setValue :forUndefinedKey:]: 这个类不符合键 keyPath 的键值编码。仅在从左向右滑动菜单时发生。我确定这不是问题,我也在真正的 iOS 设备上测试它。 if 语句中移动let mailComposeViewController = configureMailController() 行。 太棒了,它删除了警告“调用结果_未使用”。但是,黑屏仍然存在。 【参考方案1】:

请使用代码以如下方式快速打开邮件。

extension SendEmailVC: MFMailComposeViewControllerDelegate 

func contact() 

    if !MFMailComposeViewController.canSendMail() 
        ROAlert.warningAlertUnderNavigation(ROConstants.More.kNoMailConfigured, onView: self.view)
        return ()
    
    let composeVC = MFMailComposeViewController()
    composeVC.mailComposeDelegate = self
    composeVC.setToRecipients([ROConstants.More.kContactEmail])
    // Present the view controller modally.
    self.present(composeVC, animated: true, completion: nil)


//MARK: - MFMailComposeViewControllerDelegate

func mailComposeController(_ controller: MFMailComposeViewController,
    didFinishWith result: MFMailComposeResult, error: Error?) 
    // Check the result or perform other tasks.

    // Dismiss the mail compose view controller.
    controller.dismiss(animated: true, completion: nil)

【讨论】:

对不起,我对此有点陌生,但在从上面实施类似于您的扩展后,我仍然遇到黑屏。 @Palermox 你能否提供完整的实现,以便我可以帮助你。【参考方案2】:

这是修复黑屏问题的新更新代码。更近了 1 步。但是现在我在按下发送按钮时收到来自 showMailError() 的错误消息。这是调试器显示的内容

2018-05-14 15:03:40.474236-0400 projectName[2510:835559] [MC] 过滤捆绑 ID 的邮件表帐户:projectName,源帐户管理:1

import Foundation
import UIKit
import MessageUI

class SendEmailVC: UIViewController // MFMailComposeViewController: Caused black screen


@IBAction func SendButton_Tapped(_ sender: UIButton)


    if MFMailComposeViewController.canSendMail()
    
        let mailComposeVC = self.configureMailController()
        self.present(mailComposeVC, animated: true, completion: nil)
    
    else
    
        self.showMailError()
    


func configureMailController() -> MFMailComposeViewController

    let mailComposerVC = MFMailComposeViewController()

    mailComposerVC.setSubject("Hello")
    mailComposerVC.setMessageBody("How are you doing?", isHTML: true)
    mailComposerVC.setToRecipients(["<b>eddx544@gmail.com</b>"])
    mailComposerVC.mailComposeDelegate = self
    /*
     * mailComposerVC.addAttachmentData( attachment: date,
     mimeType: "String here",
     fileName:  "String here" )
     */
    return mailComposerVC



/*
 * DON'T EDIT THE CODE BELOW.
 */


func showMailError()

    let sendMailErrorAlert = UIAlertController(title: "Email failed to send", message: "Your device fail to send the email", preferredStyle: .alert)

    let dismiss = UIAlertAction(title: "Dismiss", style: .default, handler: nil)

    sendMailErrorAlert.addAction(dismiss)

    self.present(sendMailErrorAlert, animated: true, completion: nil)






extension SendEmailVC: MFMailComposeViewControllerDelegate

    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?)
    
        controller.dismiss(animated: true, completion: nil)
    

【讨论】:

案例完成。它工作正常。

以上是关于在发送按钮之前单击联系人按钮时出现黑屏的主要内容,如果未能解决你的问题,请参考以下文章

快速切换到 UITableViewController 时出现黑屏

加载uiview时出现黑屏

启动Windows7系统时出现黑屏现象的原因和解决方案

调用 Navigator.popUntil() 时出现黑屏

渲染到纹理时出现黑屏

iOS swiftDelegate 文件在添加代码时出现黑屏,请指导