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 ...
随机推荐
- MysQL使用一高级应用(下)
连接查询 连接查询分类如下: 表A inner join 表B:表A与表B匹配的行会出现在结果中 表A left join 表B:表A与表B匹配的行会出现在结果中,外加表A中独有的数据,未对应的数据使 ...
- monkey测试小记
本篇中不记录环境搭建,只是介绍一些经验和小秘诀吧. 一.使用安卓模拟器进行测试. 在刚刚接触到monkey测试的时候,用的真机进行测试,点击几万次甚至更多的时候,发现系统变慢了.也许是错觉,但是系统经 ...
- Matlab 实现对码功能
1.什么叫对码? 举例说明,数据库中有两张表. 表 1: 编号 描述 儿科门诊 妇科门诊 产科门诊 表 2: 编号 描述 儿科门诊 妇科门诊 产科门诊 现在要在表 1 和表 2 之间找到一一对应.比如 ...
- Linux查看和剔除当前登录用户
Linux查看和剔除当前登录用户 如何在linux下查看当前登录的用户,并且踢掉你认为应该踢掉的用户? 看了网络中的一些例子.在这里总结一下.主要用到的命令有,w,who,ps,kill,pkill ...
- LR简单解析
- 委托---.net4.0提供两个比较重要的委托
public delegate void Action<[in T1][,in T2][,in T3]......>([T1 t1][,T2 t2][,T3 t3]...) public ...
- 编码转换 Native / UTF-8 / Unicode
Native/Unicode Native 这是一个例子,this is a example Unicode 这是一个例子,this is a example Native/UTF-8 Nativ ...
- 设计模式--策略模式C++实现
策略模式C++实现 1定义 (Strategy Pattern)定义一组算法,将每个算法都封装起来,并且使他们可以相互替换 也叫政策模式 2类图 3实现 class Strategy{ protect ...
- 创建mysql表
CREATE TABLE `t_play_product` ( `product_id` ) NOT NULL AUTO_INCREMENT COMMENT '主键ID,自增', `product_n ...
- CentOS6.5 linux 逻辑卷管理 调整分区大小
[root@localhost ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 50 ...