Swift 发送邮件和发短信
// MARK: - Action
// MARK: compose mail 发送邮件
@IBAction func composeMail(sender: AnyObject) {
// 判断能否发送邮件
guard MFMailComposeViewController.canSendMail() else {
print("不能发送邮件")
return
}
let mailVC = MFMailComposeViewController()
mailVC.mailComposeDelegate = self // 代理
mailVC.setSubject("阳君") // 主题
mailVC.setToRecipients(["937447974@qq.com"]) // 收件人
mailVC.setCcRecipients(["CcRecipients@qq.com"]) // 抄送
mailVC.setBccRecipients(["bccRecipients@qq.com"]) // 密送
mailVC.setMessageBody("相关内容", isHTML: false) // 内容,允许使用html内容
if let image = UIImage(named: "qq") {
if let data = UIImagePNGRepresentation(image) {
// 添加文件
mailVC.addAttachmentData(data, mimeType: "image/png", fileName: "qq")
}
}
self.presentViewController(mailVC, animated: true, completion: nil)
}
// MARK: compose message 发送短信
@IBAction func composeMessage(sender: AnyObject) {
guard MFMessageComposeViewController.canSendText() else {
print("不能发送短信")
return
}
let messageVC = MFMessageComposeViewController()
messageVC.messageComposeDelegate = self // 代理
messageVC.recipients = ["18511056826"] // 收件人
messageVC.body = "短信内容" // 内容
// 发送主题
if MFMessageComposeViewController.canSendSubject() {
messageVC.subject = "阳君"
}
// 发送附件
if MFMessageComposeViewController.canSendAttachments() {
// 路径添加
if let path = NSBundle.mainBundle().pathForResource("Info", ofType: "plist") {
messageVC.addAttachmentURL(NSURL(fileURLWithPath: path), withAlternateFilename: "Info.plist")
}
// NSData添加
if MFMessageComposeViewController.isSupportedAttachmentUTI("public.png") {
// See [Uniform Type Identifiers Reference](https://developer.apple.com/library/ios/documentation/Miscellaneous/Reference/UTIRef/Introduction/Introduction.html)
if let image = UIImage(named: "qq") {
if let data = UIImagePNGRepresentation(image) {
// 添加文件
messageVC.addAttachmentData(data, typeIdentifier: "public.png", filename: "qq.png")
}
}
}
}
// messageVC.disableUserAttachments() // 禁用添加附件按钮
self.presentViewController(messageVC, animated: true, completion: nil)
}
// MARK: - MFMailComposeViewControllerDelegate
func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
// 关闭MFMailComposeViewController
controller.dismissViewControllerAnimated(true, completion: nil)
guard error == nil else { // 错误拦截
print(error)
return
}
switch result { // 发送状态
case MFMailComposeResultCancelled:
print("Result: Mail sending canceled") // 删除草稿
case MFMailComposeResultSaved: // 存储草稿
print("Result: Mail saved")
case MFMailComposeResultSent: // 发送成功
print("Result: Mail sent")
case MFMailComposeResultFailed: // 发送失败
print("Result: Mail sending failed")
default:// 其他
print("Result: Mail not sent")
}
}
// MARK: - MFMessageComposeViewControllerDelegate
func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult) {
print(controller.attachments) // 所有附件
// 关闭MFMessageComposeViewController
controller.dismissViewControllerAnimated(true, completion: nil)
switch result { // 发送状态
case MessageComposeResultCancelled:
print("Result: Mail sending cancelled") // 取消发送
case MessageComposeResultSent: // 发送成功
print("Result: Mail sent")
case MessageComposeResultFailed: // 发送失败
print("Result: Message sending failed")
default:// 其他
print("Result: Message not sent")
}
}
Swift 发送邮件和发短信的更多相关文章
- 打电话,发短信,发邮件,app跳转
1.打电话 - (IBAction)callPhone1:(id)sender { NSURL *url = [NSURL URLWithString:@"tel://18500441739 ...
- iOS中如何切换到发短信、打电话、发邮件
我们在做APP的时候,难免会遇到需要调用短信,电话等程序的时候.如美团. 当然,这些都只是一些简单的方法就可以实现,但是时间久了也会淡忘,所以想写这边博客.一是为了再捡起来复习一下,另一个相当于留个备 ...
- 打电话、发短信、web以及发邮件
#import "ViewController.h" #import <MessageUI/MessageUI.h> //导入信息UI库 @interface View ...
- IOS 开发,调用打电话,发短信,打开网址
IOS 开发,调用打电话,发短信,打开网址 1.调用 自带mail [[UIApplication sharedApplication] openURL:[NSURL URLWithString: ...
- html页面通过特殊链接:打电话,发短信,发邮件详细教程
采用url href链接的方式,实现在Safari ios,Android 浏览器,webos浏览器,塞班浏览器,IE,Operamini等主流浏览器,进行拨打电话功能. 1. 拨打电话 在电话号码 ...
- 移动设备wap手机网页html5通过特殊链接:打电话,发短信,发邮件详细教程
如果需要在移动浏览器中实现拨打电话,调用sms发短信,发送email等功能,移动手机WEB页面(HTML5)Javascript提供的接口是一个好办法. 采用url href链接的方式,实现在Safa ...
- IOS,发短信,发邮件,打电话
今天把APP里常用小功能 例如发短信.发邮件.打电话.全部拿出来简单说说它们的实现思路. 1.发短信实现打电话的功能,主要二种方法,下面我就分别说说它们的优缺点.1.1.发短信(1)——URL // ...
- 今天工作遇到要发短信(ios)的功能,于是随手记录了一下
ios中发送短信有两种 1.程序外调用系统短信 2.程序内调用系统发短信 第一种比较简单,直接调用url就可以了 oc下的代码为 [[UIApplication sharedApplication] ...
- iOS - 打电话, 发短信
电话.短信是手机的基础功能,iOS中提供了接口,让我们调用.这篇文章简单的介绍一下iOS的打电话.发短信在程序中怎么调用. 1.打电话 [[UIApplication sharedApplicatio ...
随机推荐
- 源码编译nginx
[root@localhost local]# yum -y install pcre pcre-devel#解压nginx源码包[root@localhost local]# tar -zxvf / ...
- webservice声明发布SOAP1.2
在不声明1.2的情况下,默认是1.1 当声明1.2时
- Bellman-Ford FORMCM
Bellman-Ford date: 2018/2/2 author:pprp theme:Dijstra 简介 单源最短路问题 要求: 图中不能出现负圈 思路: Bellman-Ford算法就是遍历 ...
- 那些年java MD5加密字符编码的坑
相信做过MD5加密的童鞋都遇到过字符编码的坑,一般加密出来的结果和其他人不一样都是字符编码不一致导致的,比如类文件的字符编码.浏览器的字符编码等和对方不一致,所以就需要转码统一字符. 以下是笔者转码过 ...
- geoserver源码学习与扩展——跨域访问配置
在 geoserver源码学习与扩展——restAPI访问 博客中提到了geoserver的跨域参数设置,本文详细讲一下geoserver的跨域访问配置. geoserver的跨域访问依赖java-p ...
- 关于Eclipse SVN 分支 与主干 小结
SVN建立分支和合并代码 https://blog.csdn.net/luofeixiongsix/article/details/52052631 SVN创建指定版本号的分支 https://blo ...
- Python基础笔记系列十二:requests模块的简单应用
本系列教程供个人学习笔记使用,如果您要浏览可能需要其它编程语言基础(如C语言),why?因为我写得烂啊,只有我自己看得懂!! httpbin httpbin这个网站能测试 HTTP 请求和响应的各种信 ...
- JavaScript高级程序设计-读书笔记(1)
第1章 JavaScript简介 JavaScript是一种专为与网页交互而设计的脚本语言,由下列三个不同的部分组成: l ECMAScript:提供核心语言功能: l 文 ...
- Vuejs methods how to use
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- 【Python】关于使用pycharm遇到只能使用unittest方式运行,无法直接选择Run
相信大家可能都遇到过这个问题,使用pycharm直接运行脚本的时候,只能选择unittest的方式,能愁死个人