SwiftUI: Send email

前端 未结 6 1502
小蘑菇
小蘑菇 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 03:12

    Answers are correct Hobbes the Tige & Matteo

    From the comments, if you need to show an alert if no email is set up on the button or tap gesture

    @State var isShowingMailView = false
    @State var alertNoMail = false
    
    
    HStack {
                    Image(systemName: "envelope.circle").imageScale(.large)
                    Text("Contact")
                }.onTapGesture {
                    MFMailComposeViewController.canSendMail() ? self.isShowingMailView.toggle() : self.alertNoMail.toggle()
                }
                    //            .disabled(!MFMailComposeViewController.canSendMail())
                    .sheet(isPresented: $isShowingMailView) {
                        MailView(result: self.$result)
                }
                .alert(isPresented: self.$alertNoMail) {
                    Alert(title: Text("NO MAIL SETUP"))
                }
    

    To pre-populate To, Body ...

    func makeUIViewController(context: UIViewControllerRepresentableContext) -> MFMailComposeViewController {
            let vc = MFMailComposeViewController()
            vc.setToRecipients(["your@mail.com"])
            vc.setMessageBody("

    You're so awesome!

    ", isHTML: true) vc.mailComposeDelegate = context.coordinator return vc }

提交回复
热议问题