在UIViewController
Swift 的正常情况下,我使用此代码发送邮件。
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中实现相同目标?
我需要使用UIViewControllerRepresentable
吗?
如前所述,您需要将组件移植到SwiftUI
via UIViewControllerRepresentable
。
这是一个简单的实现:
struct MailView: UIViewControllerRepresentable {
@Binding var isShowing: Bool
@Binding var result: Result?
class Coordinator: NSObject, MFMailComposeViewControllerDelegate {
@Binding var isShowing: Bool
@Binding var result: Result?
init(isShowing: Binding,
result: Binding?>) {
_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) -> MFMailComposeViewController {
let vc = MFMailComposeViewController()
vc.mailComposeDelegate = context.coordinator
return vc
}
func updateUIViewController(_ uiViewController: MFMailComposeViewController,
context: UIViewControllerRepresentableContext) {
}
}
用法:
struct ContentView: View {
@State var result: Result? = 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"))
}
}
注意事项:
我使用a ZStack
来显示它,因为Modal
行为非常不一致。
(已在运行iOS 13的iPhone 7 Plus上进行了测试-就像一个护身符)
更新了Xcode 11 beta 5