首页 > 解决方案 > SwiftUI mailComposer 视图

问题描述

有什么方法可以在 SwiftUI MFMailComposeViewController 上启用附件功能?在我的 SwiftUI 应用程序中显示时,它接受文本,但不允许粘贴图像,并且不显示用于添加附件的按钮。是否可以启用更完整的功能,更像是您使用 Apple 邮件或 Gmail 获得的完整标准邮件编辑器?谢谢

import Foundation
import UIKit
import MessageUI
import SwiftUI

struct MailView: UIViewControllerRepresentable {
    @EnvironmentObject var theBody: GlobalData
    var imageData: NoteImageData


    @Binding var isShowing: Bool
    @Binding var result: Result<MFMailComposeResult, Error>?

    class Coordinator: NSObject, MFMailComposeViewControllerDelegate {
   
        @Binding var isShowing: Bool
        @Binding var result: Result<MFMailComposeResult, Error>?

        init(isShowing: Binding<Bool>,
             result: Binding<Result<MFMailComposeResult, Error>?>) {
            _isShowing = isShowing
            _result = result
        }

        func mailComposeController(_ controller: 
        MFMailComposeViewController,
                               didFinishWith result: 
        MFMailComposeResult,
                               error: Error?) {
        defer {
            isShowing = false
        }
        guard error == nil else {
            self.result = .failure(error!)
            return
        }
        if result == MFMailComposeResult(rawValue: 2){
            print("sent")
            playSound(sound: "mailSent", type: "mp3")
        }
        if result == MFMailComposeResult(rawValue: 0){
            print("----------------> cancelled <----------------")
            
           
        }
       
        controller.dismiss(animated: true, completion: nil)
        //self.result = .success(result)
    }
}


func makeCoordinator() -> Coordinator {
    return Coordinator(isShowing: $isShowing,
                       result: $result)
}

func makeUIViewController(context: 
    UIViewControllerRepresentableContext<MailView>) -> 
    MFMailComposeViewController {
    let vc = MFMailComposeViewController()
    vc.mailComposeDelegate = context.coordinator

    vc.setSubject("")
    //vc.setMessageBody(imageData.description , isHTML: true)
    vc.setMessageBody( theBody.mailBody, isHTML: true)
    
    self.theBody.mailSuccess = true
    theBody.mailBody = "" //12/26
 
    return vc
}

    func updateUIViewController(_ uiViewController: 
    MFMailComposeViewController,
        context: UIViewControllerRepresentableContext<MailView>) {
    print("==============> update UIviewController called")
 }
}

标签: swiftuimfmailcomposeviewcontroller

解决方案


推荐阅读