我正在尝试在我的SwiftUI应用程序中实现“发送电子邮件”按钮,使用SwiftUI生命周期并针对iOS 14进行目标设置。
我知道网上有很多解决方案-在这里和其他地方都有。然而,到目前为止我还没有能够让任何东西在模拟器/设备上工作。
我的当前解决方案看起来像这样(基于这个stackoverflow问题:
模拟器显示了按钮,并且没有任何错误提示。但是,点击按钮时什么也不会发生 - 在设备上测试时也是同样的情况。有什么想法吗?谢谢!
我知道网上有很多解决方案-在这里和其他地方都有。然而,到目前为止我还没有能够让任何东西在模拟器/设备上工作。
我的当前解决方案看起来像这样(基于这个stackoverflow问题:
import SwiftUI
import MessageUI
import Foundation
struct ContentView: View {
class MailComposeViewController: UIViewController, MFMailComposeViewControllerDelegate {
static let shared = MailComposeViewController()
func sendEmail() {
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setToRecipients(["test@test.com"])
UIApplication.shared.windows.last?.rootViewController?.present(mail, animated: true, completion: nil)
} else {
// Alert
}
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
}
var body: some View {
Button(action: {
MailComposeViewController.shared.sendEmail()
}, label: {
Text("Send")
})
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
模拟器显示了按钮,并且没有任何错误提示。但是,点击按钮时什么也不会发生 - 在设备上测试时也是同样的情况。有什么想法吗?谢谢!
MFMailComposeViewController.canSendMail()
返回true。 - Arjun