Swift如何为设置中心添加常用功能

2020-01-09 00:09:19刘景俊

再需要添加邮件系统提示和邮件发送检测。


//邮件系统提示
func showSendMailErrorAlert() {
 let sendMailErrorAlert = UIAlertController(title: NSLocalizedString("Unable To Send", comment: "Unable To Send"), message: NSLocalizedString("Your device has not been set up, please set in the mail application and then try to send.", comment: "Your device has not been set up, please set in the mail application and then try to send."), preferredStyle: .alert)
 sendMailErrorAlert.addAction(UIAlertAction(title: NSLocalizedString("Confirm", comment: "Confirm action title"), style: .default) { _ in })
 self.present(sendMailErrorAlert, animated: true){}

}
//邮件发送检测
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {

 switch result.rawValue {
 case MFMailComposeResult.cancelled.rawValue:
 print("取消发送")
 case MFMailComposeResult.sent.rawValue:
 print("发送成功")
 default:
 break
 }
 self.dismiss(animated: true, completion: nil)
}

最后我们在调用邮件反馈的地方,需要先判断是否能够发送,如果不能发送通过提示信息告诉用户失败原因,如果可以发送将成功调取发送窗口。 在需要邮件反馈的地方:


if MFMailComposeViewController.canSendMail() {
 //注意这个实例要写在if block里,否则无法发送邮件时会出现两次提示弹窗(一次是系统的)
 let mailComposeViewController = configuredMailComposeViewController()
 self.present(mailComposeViewController, animated: true, completion: nil)

} else {
 self.showSendMailErrorAlert()
}

swift,跳转,appstore

三、系统分享功能

分享前,我们需要设置好分享的信息:标题、图片、链接。


var webUrl:String = "https://www.easck.com/cn/app/id1355476695"
var urlTitle:String = "OneScreen"
var urlImage:UIImage = #imageLiteral(resourceName: "onescreen_icon")

这里使用了var,是为了在特殊情况下改变他们的值,具体的调用方式如下:


let shareVC:UIActivityViewController = UIActivityViewController(activityItems: [self.urlTitle,self.urlImage,self.webUrl], applicationActivities: nil)

self.present(shareVC, animated: true, completion: {
 print("shareVC success")
})

swift,跳转,appstore

四、打开某些网址

打开网址可以实现“官方网址”、“应用更新说明”功能,更新说明我们可以通过更新Web内容快速高速用户更新列表。如果你的应用需要比较多的教程,也可以通过网页的形式展现。为了方便用户反馈,我通常会增加一个微博入口,让用户打开微博地址快速与我联系进行反馈。