How to open mail app from Swift

前端 未结 15 1340
被撕碎了的回忆
被撕碎了的回忆 2020-11-30 20:18

Im working on a simple swift app where the user inputs an email address and presses a button which opens the mail app, with the entered address in the address bar. I know ho

15条回答
  •  盖世英雄少女心
    2020-11-30 20:53

    Here how it looks for Swift 4:

    import MessageUI
    
    if MFMailComposeViewController.canSendMail() {
        let mail = MFMailComposeViewController()
        mail.mailComposeDelegate = self
        mail.setToRecipients(["test@test.test"])
        mail.setSubject("Bla")
        mail.setMessageBody("Blabla", isHTML: true)
        present(mail, animated: true, completion: nil)
    } else {
        print("Cannot send mail")
        // give feedback to the user
    }
    
    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
            switch result.rawValue {
            case MFMailComposeResult.cancelled.rawValue:
                print("Cancelled")
            case MFMailComposeResult.saved.rawValue:
                print("Saved")
            case MFMailComposeResult.sent.rawValue:
                print("Sent")
            case MFMailComposeResult.failed.rawValue:
                print("Error: \(String(describing: error?.localizedDescription))")
            default:
                break
            }
            controller.dismiss(animated: true, completion: nil)
        }
    

提交回复
热议问题