2016-11-27 84 views
0

我有一个通过UIButton触发的UIAlertView。通过UIAlertView按钮打开iOS电子邮件应用程序

UIAlertView显示两个按钮,“打开电子邮件”和“取消”。

“取消”从视图中删除UIAlert。我试图这样做,当用户点击“打开电子邮件”时,他们的设备将打开默认的电子邮件应用程序的撰写屏幕,其中的电子邮件地址已在“发件人”部分。

使用斯威夫特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

首先,我想什么你需要在你的代码片段添加仅这一部分:

@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:)的文档:

处理机

当用户选择的操作执行以下步骤的块。该 块没有返回值,并将选定的操作对象作为其仅参数 。

所以,你openEmail应该是这样的:

let openEmail = UIAlertAction(title: "Open Email", style: .destructive, handler: { (actionSheetController) in 
      // send your email here... 

     }) 

我不是很确定的机制是如何你想发送一封电子邮件,但我THIK你可能要检查MFMailComposeViewControllerthis question应该可以帮助你使用它。

希望它有帮助。

+0

非常感谢!对于如何将MFMailCompose代码添加到UIAlertAction中的处理程序有点困惑。它在连接到UIButton时效果很好,但在UIAlertAction中放入处理程序时似乎不起作用。 – Miles

+0

在按钮操作中做什么,但在关闭中(同样的“在此处发送您的电子邮件......”存在的块)的操作完全相同。 –

+0

现在工作!谢谢。我不得不加上“自我”。在“present(mail,animated:true)”行上,当电子邮件屏幕打开并且我点击“取消”或发送电子邮件时,邮件视图不会消失。 – Miles

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 protected]" 
     let url = NSURL(string: "mailto:\("[email protected]")") 
     UIApplication.shared.openURL(url as! URL)}) 


    alertController.addAction(openEmail) 
    alertController.addAction(cancel) 


    self.present(alertController, animated: true, completion: nil) 



} 
+0

看起来你已经找到了答案,但如果你想扩展您的功能,包括其他电子邮件客户端比标准的iOS邮件应用程序,我写了一篇博客文章,详细说明究竟是如何在这里做到这一点:HTTPS: //medium.com/@harrybloom18/ios-email-picker-99045d940a3d –

+0

@蜂巢喧闹非常感谢您! – Miles

2

你必须使用MFMailComposer发送电子邮件,这是你如何使用它

if MFMailComposeViewController.canSendMail() { 
     let mail = MFMailComposeViewController() 
     mail.mailComposeDelegate = self 
     mail.setToRecipients(["[email protected]"]) 
     mail.setMessageBody("you email body", isHTML: false) 
     present(mail, animated: true) 
    } else { 
     // email is not added in app 
    } 

而且import MessageUI框架,符合MFMailComposeViewControllerDelegate

此外,您不需要像你在回答中提到的那样使用openURL,因为你正在将用户从应用程序重定向到另一个应用程序,因此不需要使用MFMailComposer

+0

我会试试这个!谢谢。我如何符合MFMailComposeViewControllerDelegate? – Miles

+0

类的ViewController:视图控制器,MFMailComposeViewControllerDelegate { – Rajat

+0

感谢。这在UIButton上运行良好,但我试图弄清楚如何将它附加到UIAlertView中的按钮。 (UIAlertAction)。 – Miles

相关问题