SwiftUI 和 UIViewControllerRepresentable - 在尝试转换类型值时发现意外的空指针值

问题描述 投票:0回答:0

我试图在我的 SwiftUI 应用程序中显示一个

MFMailComposeViewController
,但我收到了错误消息。

尝试转换类型值时发现意外的空指针值 'MFMailComposeViewController' (0x1272f5248) 到 'UIViewController' (0x11fdb4418)

斯威夫特用户界面

struct ListView: View {

@State var isShowingMailView = false
@State var email = "[email protected]"

var body: some View {
    ScrollView(showsIndicators: isMac ? true : false) {
        HStack(spacing: 16) {
          Text("Support")
          Spacer()
        }
       .onTapGesture {
          self.isShowingMailView.toggle()
       }
       .disabled(!MFMailComposeViewController.canSendMail())
       .sheet(isPresented: $isShowingMailView) {
           MailView(email: email)
       }

       Spacer()

    } // SCROLLVIEW

}

}

邮件查看

struct MailView : UIViewControllerRepresentable{
    
    var email: String
    
    typealias UIViewControllerType = MFMailComposeViewController
    
    func updateUIViewController(_ uiViewController: MFMailComposeViewController, context: Context) {
        
    }
    
    func makeUIViewController(context: Context) -> MFMailComposeViewController {
        if MFMailComposeViewController.canSendMail(){
            let view = MFMailComposeViewController()
            view.mailComposeDelegate = context.coordinator
            view.setToRecipients([email])
            return view
        } else {
            return MFMailComposeViewController()
        }
    }
    
    func makeCoordinator() -> Coordinator {
        return Coordinator(self)
    }
    
    
    class Coordinator : NSObject, MFMailComposeViewControllerDelegate{
        
        var parent : MailView
        
        init(_ parent: MailView){
            self.parent = parent
        }
        
        func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
            controller.dismiss(animated: true)
        }
        
        
    }
    
    
}
swiftui mfmailcomposeviewcontroller uiviewcontrollerrepresentable
© www.soinside.com 2019 - 2024. All rights reserved.