SwiftUI:发送电子邮件 [英] SwiftUI: Send email

查看:218
本文介绍了SwiftUI:发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Swift的普通UIViewController中,我使用此代码发送邮件.

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

let mailComposeViewController = configuredMailComposeViewController()

mailComposeViewController.navigationItem.leftBarButtonItem?.style = .plain
mailComposeViewController.navigationItem.rightBarButtonItem?.style = .plain
mailComposeViewController.navigationBar.tintColor = UIColor.white

if MFMailComposeViewController.canSendMail() {
    self.present(mailComposeViewController, animated: true, completion: nil)
} else {
    self.showSendMailErrorAlert()
}

如何在SwiftUI中实现相同的目标?

How can I achieve the same in SwiftUI?

我需要使用UIViewControllerRepresentable吗?

推荐答案

如前所述,您需要通过UIViewControllerRepresentable将组件移植到SwiftUI.

As you mentioned, you need to port the component to SwiftUI via UIViewControllerRepresentable.

这是一个简单的实现:

struct MailView: UIViewControllerRepresentable {

    @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
            }
            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
        return vc
    }

    func updateUIViewController(_ uiViewController: MFMailComposeViewController,
                                context: UIViewControllerRepresentableContext<MailView>) {

    }
}


用法:

struct ContentView: View {

    @State var result: Result<MFMailComposeResult, Error>? = nil
    @State var isShowingMailView = false

    var body: some View {
        ZStack {
            VStack {
                Button(action: {
                    self.isShowingMailView.toggle()
                }) {
                    Text("Show mail view")
                }
                if result != nil {
                    Text("Result: \(String(describing: result))")
                    .lineLimit(nil)
                }
            }
            if (isShowingMailView) {
                mailView()
                .transition(.move(edge: .bottom))
                .animation(.default)
            }
        }


    }

    private func mailView() -> some View {
        MFMailComposeViewController.canSendMail() ?
            AnyView(MailView(isShowing: $isShowingMailView, result: $result)) :
            AnyView(Text("Can't send emails from this device"))
    }
}


注释:

我正在使用ZStack进行显示,因为Modal行为非常不一致.

I'm using a ZStack to show it, as the Modal behaviour was quite inconsistent.

(在运行iOS 13的iPhone 7 Plus上进行了测试-像魅惑一样工作)

(Tested on iPhone 7 Plus running iOS 13 - works like a charm)

已针对Xcode 11 beta 5更新

这篇关于SwiftUI:发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆