SwiftUI: Send email

前端 未结 6 1497
小蘑菇
小蘑菇 2020-12-08 02:06

In a normal UIViewController in Swift, I use this code to send a mail.

let mailComposeViewController = configuredMailComposeViewController()

ma         


        
6条回答
  •  抹茶落季
    2020-12-08 02:57

    I've created a github repository for it. just add it to your project and use it like this:

    struct ContentView: View {
    
    @State var showMailSheet = false
    
    var body: some View {
        NavigationView {
            Button(action: {
                self.showMailSheet.toggle()
            }) {
                Text("compose")
            }
        }
        .sheet(isPresented: self.$showMailSheet) {
            MailView(isShowing: self.$showMailSheet,
                     resultHandler: {
                        value in
                        switch value {
                        case .success(let result):
                            switch result {
                            case .cancelled:
                                print("cancelled")
                            case .failed:
                                print("failed")
                            case .saved:
                                print("saved")
                            default:
                                print("sent")
                            }
                        case .failure(let error):
                            print("error: \(error.localizedDescription)")
                        }
            },
                     subject: "test Subjet",
                     toRecipients: ["recipient@test.com"],
                     ccRecipients: ["cc@test.com"],
                     bccRecipients: ["bcc@test.com"],
                     messageBody: "works like a charm!",
                     isHtml: false)
            .safe()
            
        }
    
      }
    }
    

    safe() modifier checks if MFMailComposeViewController.canSendMail() is false, it automatically dismesses the modal and tries to open a mailto link.

提交回复
热议问题