MFMessageComposeViewController + SwiftUI 错误行为

MFMessageComposeViewController + SwiftUI Buggy Behavior

我正在使用 ViewControllerRepresentable 来呈现 MFMessageComposeViewController,以便用户可以从我的应用程序发送文本。

但是,每次显示视图时,都会出现很多错误 - 元素随机消失,滚动关闭,屏幕闪烁。在 iOS 14.2 和 14.3 上测试。

代码如下:

import SwiftUI
import MessageUI

struct MessageView: UIViewControllerRepresentable {
    var recipient: String
    
    class Coordinator: NSObject, MFMessageComposeViewControllerDelegate {
        var completion: () -> Void
        init(completion: @escaping ()->Void) {
            self.completion = completion
        }
        
        // delegate method
        func messageComposeViewController(_ controller: MFMessageComposeViewController,
                                   didFinishWith result: MessageComposeResult) {
            controller.dismiss(animated: true, completion: nil)
            completion()
        }
    }

    func makeCoordinator() -> Coordinator {
        return Coordinator() {} // not using completion handler
    }
    
    func makeUIViewController(context: Context) -> MFMessageComposeViewController {
        let vc = MFMessageComposeViewController()
        vc.recipients = [recipient]

        vc.messageComposeDelegate = context.coordinator
        return vc
    }
    
    func updateUIViewController(_ uiViewController: MFMessageComposeViewController, context: Context) {}
    
    typealias UIViewControllerType = MFMessageComposeViewController
}

和我的观点

struct ContentView: View {
    @State private var isShowingMessages = false
    @State var result: Result<MFMailComposeResult, Error>? = nil
    var body: some View {
        VStack {
            Button("Show Messages") {
                self.isShowingMessages = true
            }
            .sheet(isPresented: self.$isShowingMessages) {
            MessageView(recipient: "+15555555555")
        }
            .edgesIgnoringSafeArea(.bottom)
        }
    }
}

我表达这个观点的方式有问题吗?有没有其他人经历过这种行为?类似的行为发生在 MFMailComposeViewController 上,但没有那么多问题。

5 分钟后,我意识到我需要在呈现 sheet 时添加此内容:

MessageView(recipient: "+15555555555")
    .ignoresSafeArea()

该视图看起来有问题,因为它试图解释键盘安全区域并且很难做到。