用Swift写一个发送邮件的iOS用户反馈

前端之家收集整理的这篇文章主要介绍了用Swift写一个发送邮件的iOS用户反馈前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

为了接收用户反馈,很多iOS应用都会在设置页面中,加入发送邮件功能——尤其当应用是由个人开发者开发时。当然iOS中邮件的发送方式有很多种,有体验相对较差openURL跳转方式,也有调用其他第三方库等办法。

不过较常用且方便的,还是如下图(应用为潮汐),调用系统的MFMailComposeViewController视图在应用内完成邮件发送,并返回应用。

chaoxi_Feedback

下面就详解下这种方式的实现步骤。

一、建立静态列表

首先,拖一个Table View Controller到main.storyboard中,并选中Table View在右侧属性面板中将其设置为静态列表Static Cells。

static_cells

为了演示方便这里就先创建一个Section,其中有两行Cell。两个Cell的Style都设置为Basic,并将Title修改如下。

table_view_cel

下一步是建立这个Table View的Controller。新建一个Cocoa Touch Class文件,并选择Subclass of UITableViewController。

接着在右边工具栏面板中为其设置好Custom Class。由于这里暂时用不到这个UITableViewController类里的内容,可以把他们都注释掉或删掉。接着在其中重写一个tableView点选的函数

  1. override func tableView(tableView: UITableView,didSelectRowAtIndexPath indexPath: NSIndexPath){
  2.  
  3. if indexPath.section == 0 && indexPath.row == 0 {
  4. print("给应用评分")
  5. }
  6.  
  7. if indexPath.section == 0 && indexPath.row == 1 {
  8. print("意见反馈")
  9. }
  10. }

在模拟器中运行,点按Cell,检查output区中print的内容是否正常,然后就可以进入下一步。

二、MFMailComposeViewController

处理完UITableViewController以后,就可以开始调用邮件视图了。不过先不急着写代码,首先需要导入框架MessageUI.framework。在项目设置Build Phases的Link Binary With Libraries中添加MessageUI.framework。

messageui

然后在Controller里导入头文件import MessageUI。并给Controller加上MFMailComposeViewControllerDelegate协议。

上述步骤搞定后,就可以愉快地写代码了。首先先写个函数,来配置发邮件的视窗。

  1. func configuredMailComposeViewController() -> MFMailComposeViewController {
  2.  
  3. let mailComposeVC = MFMailComposeViewController()
  4. mailComposeVC.mailComposeDelegate = self
  5.  
  6. //设置邮件地址、主题及正文
  7. mailComposeVC.setToRecipients(["<你的邮箱地址>"])
  8. mailComposeVC.setSubject("<邮件主题>")
  9. mailComposeVC.setMessageBody("<邮件正文>",isHTML: false)
  10.  
  11. return mailComposeVC
  12.  
  13. }

鉴于这种发送邮件的方式,要求用户已经在设备上至少添加有一个邮箱,所以对没有设置邮箱的用户,还应予以提示。因此这里再写一个函数,来配置针对未设置邮箱用户的弹窗提醒。

  1. func showSendMailErrorAlert() {
  2.  
  3. let sendMailErrorAlert = UIAlertController(title: "无法发送邮件",message: "您的设备尚未设置邮箱,请在“邮件”应用中设置后再尝试发送。",preferredStyle: .Alert)
  4. sendMailErrorAlert.addAction(UIAlertAction(title: "确定",style: .Default) { _ in })
  5. self.presentViewController(sendMailErrorAlert,animated: true){}
  6.  
  7. }

搞定这俩函数后,就可以在之前的tableView函数调用两者了。

  1. if indexPath.section == 0 && indexPath.row == 1 {
  2. print("意见反馈")
  3.  
  4. if MFMailComposeViewController.canSendMail() {
  5. //注意这个实例要写在if block里,否则无法发送邮件时会出现两次提示弹窗(一次是系统的)
  6. let mailComposeViewController = configuredMailComposeViewController()
  7. self.presentViewController(mailComposeViewController,animated: true,completion: nil)
  8. } else {
  9. self.showSendMailErrorAlert()
  10. }
  11.  
  12. }

最后,写上dismiss邮件视窗的函数,就大功告成了。

  1. func mailComposeController(controller: MFMailComposeViewController,didFinishWithResult result: MFMailComposeResult,error: NSError?) {
  2.  
  3. switch result.rawValue {
  4. case MFMailComposeResultCancelled.rawValue:
  5. print("取消发送")
  6. case MFMailComposeResultSent.rawValue:
  7. print("发送成功")
  8. default:
  9. break
  10. }
  11. self.dismissViewControllerAnimated(true,completion: nil)
  12.  
  13. }

三、加入设备及应用信息

为了获得更加准确的反馈信息,可以在邮件正文里加入反馈者的设备及应用信息。那怎样使用swift获得设备信息呢?可以如下通过UIDevice取得。

  1. //获取设备名称
  2. let deviceName = UIDevice.currentDevice().name
  3. //获取系统版本号
  4. let systemVersion = UIDevice.currentDevice().systemVersion
  5. //获取设备的型号
  6. let deviceModel = UIDevice.currentDevice().model
  7. //获取设备唯一标识符
  8. let deviceUUID = UIDevice.currentDevice().identifierForVendor?.UUIDString

这里的设备型号deviceModel只能获知设备的简单区分(如是iPhone还是iPad),如果需要详细的iOS设备信息,还需要写一个UIDevice的扩展。

  1. public extension UIDevice {
  2.  
  3. var modelName: String {
  4. var systemInfo = utsname()
  5. uname(&systemInfo)
  6. let machineMirror = Mirror(reflecting: systemInfo.machine)
  7. let identifier = machineMirror.children.reduce("") { identifier,element in
  8. guard let value = element.value as? Int8 where value != 0 else { return identifier }
  9. return identifier + String(UnicodeScalar(UInt8(value)))
  10. }
  11.  
  12. switch identifier {
  13. case "iPod5,1": return "iPod Touch 5"
  14. case "iPod7,1": return "iPod Touch 6"
  15. case "iPhone3,1","iPhone3,2",3": return "iPhone 4"
  16. case "iPhone4,1": return "iPhone 4s"
  17. case "iPhone5,"iPhone5,2": return "iPhone 5"
  18. case "iPhone5,3",4": return "iPhone 5c"
  19. case "iPhone6,"iPhone6,2": return "iPhone 5s"
  20. case "iPhone7,2": return "iPhone 6"
  21. case "iPhone7,1": return "iPhone 6 Plus"
  22. case "iPhone8,1": return "iPhone 6s"
  23. case "iPhone8,2": return "iPhone 6s Plus"
  24. case "iPad2,"iPad2,4":return "iPad 2"
  25. case "iPad3,"iPad3,3": return "iPad 3"
  26. case "iPad3,4",5",6": return "iPad 4"
  27. case "iPad4,"iPad4,3": return "iPad Air"
  28. case "iPad5,"iPad5,4": return "iPad Air 2"
  29. case "iPad2,6",7": return "iPad Mini"
  30. case "iPad4,6": return "iPad Mini 2"
  31. case "iPad4,7",8",9": return "iPad Mini 3"
  32. case "iPad5,2": return "iPad Mini 4"
  33. case "iPad6,"iPad6,8": return "iPad Pro"
  34. case "AppleTV5,3": return "Apple TV"
  35. case "i386","x86_64": return "Simulator"
  36. default: return identifier
  37. }
  38. }
  39.  
  40. }
  41.  
  42. //调用
  43. let modelName = UIDevice.currentDevice().modelName

获取这些设备信息后,就可以在邮件正文中加入它们了,比如:

  1. mailComposeVC.setMessageBody("\\n\\n\\n系统版本:\\(systemVersion)\\n设备型号:\\(modelName)",isHTML: false)

同理,也可以获得应用的相关信息。

  1. let infoDic = NSBundle.mainBundle().infoDictionary
  2.  
  3. // 获取App的版本号
  4. let appVersion = infoDic?["CFBundleShortVersionString"]
  5. // 获取App的build版本
  6. let appBuildVersion = infoDic?["CFBundleVersion"]
  7. // 获取App的名称
  8. let appName = infoDic?["CFBundleDisplayName"]

到这里,一个调用UITableViewController的iOS邮件反馈就基本写完了。运行的时候,要注意用虚拟器的话可能会报错,测试需要真机环境。效果如下。

猜你在找的Swift相关文章