我有一个通过UIButton触发的UIAlertView。
UIAlertView显示两个按钮,“打开电子邮件”和“取消”。
“取消”会从视图中删除UIAlert。我试图这样做,当用户点击“打开电子邮件”时,他们的设备会打开默认电子邮件应用程序的撰写屏幕,电子邮件地址已经在“发件人”部分。
使用Swift 3.
谢谢!
import UIKit
import Kingfisher
class SettingsViewController: UIViewController {
@IBAction func copyrightInfo(_ sender: Any) {
let alert = UIAlertController(title: "Copyright Info", message: "text", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "I understand", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
@IBAction func helpfeedbackAlert(_ sender: Any) {
let alertController = UIAlertController(title: "Help & Feedback", message: "text", preferredStyle: .alert)
let cancel = UIAlertAction(title: "Cancel", style: .destructive, handler: nil)
let openEmail = UIAlertAction(title: "Open Email", style: .default, handler: nil)
alertController.addAction(openEmail)
alertController.addAction(cancel)
self.present(alertController, animated: true, completion: nil)
}
@IBAction func clearCache(_ sender: Any) {
// SDImageCache.shared().clearMemory()
// SDImageCache.shared().clearDisk()
// Clear memory cache right away.
ImageCache.default.clearMemoryCache()
// Clear disk cache. This is an async operation.
ImageCache.default.clearDiskCache()
}
@IBAction func rateApp(_ sender: Any) {
if let url = URL(string: "https://www.google.com") {
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:]) {
boolean in
// do something with the boolean
}
} else {
// Fallback on earlier versions
}
}
}
@IBAction func purchasePhotos(_ sender: Any) {
if let url = URL(string: "https://google.com") {
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:]) {
boolean in
// do something with the boolean
}
} else {
// Fallback on earlier versions
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override var prefersStatusBarHidden: Bool {
get {
return true
}
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
答案 0 :(得分:2)
您必须使用MFMailComposer
发送电子邮件,这就是您使用它的方式
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setToRecipients(["rajatpagare@hotmail.com"])
mail.setMessageBody("you email body", isHTML: false)
present(mail, animated: true)
} else {
// email is not added in app
}
同样import MessageUI
框架并符合MFMailComposeViewControllerDelegate
此外,您不需要像在答案中提到的那样使用openURL,因为您正在将用户从应用程序重定向到另一个应用程序,没有必要这样做,您可以使用MFMailComposer
。
答案 1 :(得分:0)
我使用了这段代码:
@IBAction func helpfeedbackAlert(_ sender: Any) {
let alertController = UIAlertController(title: "Help & Feedback", message: "text", preferredStyle: .alert)
let cancel = UIAlertAction(title: "Cancel", style: .destructive, handler: nil)
let openEmail = UIAlertAction(title: "Open Email", style: .default, handler: { (actionSheetController) -> Void in let email = "email@example.com"
let url = NSURL(string: "mailto:\("email@example.com")")
UIApplication.shared.openURL(url as! URL)})
alertController.addAction(openEmail)
alertController.addAction(cancel)
self.present(alertController, animated: true, completion: nil)
}
答案 2 :(得分:0)
首先,我假设你需要在你的代码片段中添加什么只是这一部分:
@IBAction func helpfeedbackAlert(_ sender: Any) {
let alertController = UIAlertController(title: "Help & Feedback", message: "text", preferredStyle: .alert)
let cancel = UIAlertAction(title: "Cancel", style: .destructive, handler: nil)
let openEmail = UIAlertAction(title: "Open Email", style: .default, handler: nil)
alertController.addAction(openEmail)
alertController.addAction(cancel)
self.present(alertController, animated: true, completion: nil)
}
无论如何,在创建handler
的实例时,您需要填写UIAlertAction
。参考init(title:style:handler:)的文档:
处理程序
用户选择操作时要执行的块。这个 block没有返回值,并将选定的操作对象作为其 只有参数。
因此,您的openEmail
应该是:
let openEmail = UIAlertAction(title: "Open Email", style: .destructive, handler: { (actionSheetController) in
// send your email here...
})
我不太确定您希望如何发送电子邮件的机制,但我想您可以查看MFMailComposeViewController,this question应该可以帮助您解决这个问题
希望它有所帮助。