我有这个标签,单击该标签可以将用户带到他们的默认电子邮件应用,并为他们撰写消息。
有人可以告诉我如何实施吗?
Let's say when someone clicks on feedback, they should be redirected to their default email app
答案 0 :(得分:0)
// email
let address = "test@test.com"
let url = URL(string: "mailto://\(address)")
UIApplication.shared.openURL(url!)
答案 1 :(得分:0)
这是发送邮件的代码。请阅读注释以了解代码
func sendEmail() {
// check if device can send mail
if MFMailComposeViewController.canSendMail() {
// create MFMailComposeViewController object
let mail = MFMailComposeViewController()
// set delegate to know when email has been sent and perform further tasks
mail.mailComposeDelegate = self
// In setToRecipients you can pass array of email to whom you want to send email
mail.setToRecipients(["someemail.com"])
// In setMessageBody you can pass the email body text
mail.setMessageBody("<p>some email text</p>", isHTML: true)
// here you can present the controller
present(mail, animated: true)
} else {
// show failure alert
//Unable to send Email(email may not be configured in your device)
// or may be you are using simulator
}
}
现在,您可以使用委托方法关闭MFMailComposeViewController
extension YourViewController : MFMessageComposeViewControllerDelegate{
func messageComposeViewController(_ controller: MFMessageComposeViewController,
didFinishWith result: MessageComposeResult) {
// use result for further task
// Dismiss the MFMailComposeViewController.
controller.dismiss(animated: true, completion: nil)
}
}
答案 2 :(得分:0)
以下是在Swift 4上显示默认电子邮件应用程序的扩展名。
import MessageUI
extension UIViewController : MFMailComposeViewControllerDelegate {
func configuredMailComposeViewController(recipients : [String]?, subject :
String, body : String, isHtml : Bool = false,
images : [UIImage]?) -> MFMailComposeViewController {
let mailComposerVC = MFMailComposeViewController()
mailComposerVC.mailComposeDelegate = self // IMPORTANT
mailComposerVC.setToRecipients(recipients)
mailComposerVC.setSubject(subject)
mailComposerVC.setMessageBody(body, isHTML: isHtml)
//If you want to send images on Mail
for img in images ?? [] {
if let jpegData = UIImageJPEGRepresentation(img, 1.0) {
mailComposerVC.addAttachmentData(jpegData,
mimeType: "image/jpg",
fileName: "Image")
}
}
return mailComposerVC
}
func presentMailComposeViewController(mailComposeViewController :
MFMailComposeViewController) {
if MFMailComposeViewController.canSendMail() {
self.present(mailComposeViewController,
animated: true, completion: nil)
} else {
self.showErrorAlert()
}
}
public func mailComposeController(controller: MFMailComposeViewController,
didFinishWith result: MFMailComposeResult,
error: Swift.Error?) {
switch (result) {
case .cancelled:
self.dismiss(animated: true, completion: nil)
case .sent:
self.dismiss(animated: true, completion: nil)
case .failed:
self.dismiss(animated: true, completion: {
self.showErrorAlert()
})
default:
break;
}
}
func showErrorAlert() {
let sendMailErrorAlert = UIAlertController.init(title: "Failed",
message: "Unable to send email. Please check your email " +
"settings and try again.", preferredStyle: .alert)
sendMailErrorAlert.addAction(UIAlertAction.init(title: "OK",
style: .default, handler: nil))
self.present(sendMailErrorAlert,
animated: true, completion: nil)
}
}
现在您可以像这样调用这些方法。
@IBAction func didTapOpenEmailApp(sender: Any) {
let toRecipients = ["desigredMail@example.com"]
let subject = "Your subject here"
let body = "Enter comments here..."
let mail = configuredMailComposeViewController(recipients: toRecipients, subject: subject, body: body, isHtml: false, images: nil)
presentMailComposeViewController(mailComposeViewController: mail)
}
快乐编码!