开发者社区> ghost丶桃子> 正文

iOS中发送短信/发送邮件的实现

简介:
+关注继续查看
需要引入框架:

MessageUI.framework

布局如下:



短信和邮件:

[objc] view plain copy
  1. #import "ViewController.h"  
  2. #import <MessageUI/MessageUI.h>  
  3.   
  4. @interface ViewController ()<MFMessageComposeViewControllerDelegate,MFMailComposeViewControllerDelegate>//遵循协议  
  5.   
  6. @end  
  7.   
  8. @implementation ViewController  

短信功能:

[objc] view plain copy
  1. //短信功能  
  2. - (IBAction)messageButtonAction:(UIButton *)sender {  
  3. #pragma mark 程序外发送短信  
  4.       
  5.     /* 
  6.     //定义打开短信的url, 关键字: sms: 
  7.     NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"sms://%@",@"10086"]]; 
  8.     //判断程序是否可以调用打开短信功能 
  9.     if ([[UIApplication sharedApplication] canOpenURL:url]) { 
  10.         [[UIApplication sharedApplication] openURL:url]; 
  11.     }else{ 
  12.         UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您的设备不支持此短信功能" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; 
  13.         [alert show]; 
  14.     } 
  15.      */  
  16. /* 
  17.  用openURL来打开程序中的短信功能, 需要用到关键字: "sms:", 后面加上要发送的电话就可以了; 
  18.  缺点:1.这个方法会跳出我们正在运行的程序,打开系统的短信界面, 但当用户关闭短信后, 无法回到程序. 
  19.      2.这个方法我们只能定义要发送的手机号, 无法编辑发送的短信内容; 
  20.   
  21.  */  
  22.     }  

#pragma mark 程序内发送短信

    /*

     为了弥补上述的两个方法的不足,需要另一种使用短信功能的方法:程序内使用短信功能.

     */

    

    //1.添加短信所需要的框架: MessageUI.framework

    //2.引入头文件,实现如下代码

    //3.判断是否可以发短信

[objc] view plain copy
  1. - (IBAction)messageButtonAction:(UIButton *)sender {  
  2. #pragma mark 程序外发送短信  
  3.   BOOL canSendMessage = [MFMessageComposeViewController canSendText];  
  4.     if (canSendMessage) {  
  5.         //创建短信视图控制器  
  6.         MFMessageComposeViewController *messageVC = [[MFMessageComposeViewController alloc]init];  
  7.         //设置代理  
  8.         messageVC.messageComposeDelegate = self;  
  9.           
  10.         //设置短信内容  
  11.         messageVC.body = @"来一条信息";  
  12.           
  13.         //设置电话, 是一个数组, 可以设置多个电话, 实现群发功能  
  14.         messageVC.recipients = @[@"10086",@"10010"];  
  15.           
  16.         //打开短信功能, 通过这个方法会在程序内打开一个短信界面;  
  17.           
  18.         [self presentViewController:messageVC animated:YES completion:nil];  
  19.           
  20.     }else{  
  21.         UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您的设备不支持此短信功能" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil nil];  
  22.         [alert show];  
  23.     }  
  24.       
  25.       
  26. }  

信息的代理方法:

[objc] view plain copy
  1. - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{  
  2.       
  3.     //MessageComposeResult 的枚举值:  
  4. //    MessageComposeResultCancelled, //取消发送短信功能  
  5. //    MessageComposeResultSent,     //发送短信  
  6. //    MessageComposeResultFailed    //发送失败  
  7.     if (result == MessageComposeResultCancelled || result == MessageComposeResultSent) {  
  8.         [self dismissViewControllerAnimated:YES completion:nil];  
  9.     }  
  10.       
  11. }  

邮件功能:

[objc] view plain copy
  1. //邮件功能  
  2. - (IBAction)mailButtonAction:(UIButton *)sender {  
  3. #pragma mark 程序外发送邮件  
  4.       
  5.     /* 
  6.     //打开系统邮件页面, mailto: 
  7.     NSURL *mailURL = [NSURL URLWithString:[NSString stringWithFormat:@"mailto:%@",@"13683799303@163.com"]]; 
  8.     //cc:抄送对象  subject:主题  body:内容 
  9.     //NSURL *mailURL2 = [NSURL URLWithString:[NSString stringWithFormat:@"mailto:%@?cc = %@&subject = %@&body = %@",@"13683799303@163.com",@"13683799303@26.com",@"邮件",@"你好啊!"]]; 
  10.      
  11.     if ([[UIApplication sharedApplication] canOpenURL:mailURL]) { 
  12.         [[UIApplication sharedApplication] openURL:mailURL]; 
  13.     }else{ 
  14.          
  15.         UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您的设备不支持邮件功能" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; 
  16.         [alert show]; 
  17.          
  18.     } 
  19.      */  
  20.     /* 
  21.      此方法来发送邮件同上述短信一样,也会跳出程序,调用系统的邮件界面; 
  22.      */  
  23.       
  24. #pragma mark 程序内发送邮件  
  25.       
  26.     //判断是否可以发送邮件  
  27.     BOOL canSendMail = [MFMailComposeViewController canSendMail];  
  28.     if (canSendMail) {  
  29.         //创建邮件视图控制器  
  30.         MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc]init];  
  31.         //设置接收邮件人, 数组,可以实现群发  
  32.         [mailVC setToRecipients:@[@"13683799303@163.com",@"135895587@qq.com"]];  
  33.           
  34.         //设置抄送对象,  
  35.         [mailVC setCcRecipients:@[@"13683799303@163.com",@"135895587@qq.com"]];  
  36.           
  37.         //设置密送  
  38.         [mailVC setBccRecipients:@[@"13683799303@163.com"]];  
  39.           
  40.         //设置内容  
  41.         [mailVC setMessageBody:@"很高兴认识你" isHTML:NO];  
  42.           
  43.         //设置代理  
  44.         mailVC.mailComposeDelegate = self;  
  45.         //打开邮件功能  
  46.         [self presentViewController:mailVC animated:YES completion:nil];  
  47.     }else{  
  48.         UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您的设备不支持邮件功能" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil nil];  
  49.         [alert show];  
  50.           
  51.     }  
  52.       
  53.       
  54. }  

邮件代理的方法:

[objc] view plain copy
  1. - (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{  
  2.     //    MFMailComposeResultCancelled,  取消发送  
  3.     //    MFMailComposeResultSaved,      保存  
  4.     //    MFMailComposeResultSent,       发送  
  5.     //    MFMailComposeResultFailed      发送失败  
  6.       
  7.     switch (result) {  
  8.         case MFMailComposeResultCancelled:  
  9.             NSLog(@"取消发送");  
  10.             break;  
  11.         case MFMailComposeResultSaved:  
  12.             NSLog(@"保存");  
  13.             break;  
  14.         case MFMailComposeResultSent:  
  15.             NSLog(@"发送成功");  
  16.             break;  
  17.         case MFMailComposeResultFailed:  
  18.             NSLog(@"失败");  
  19.             break;  
  20.               
  21.         default:  
  22.             break;  
  23.     }  
  24.       
  25.     [self dismissViewControllerAnimated:YES completion:nil];  
  26.       
  27. }  

最终效果:(由于模拟器没法演示发送短信,所以会出现下面的现象)


每日更新关注:http://weibo.com/hanjunqiang  新浪微博


原文地址:http://blog.csdn.net/qq_31810357/article/details/49990635

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
weex-自定义module,实现weex在iOS的本地化,js之间互相跳转,交互,传值(iOS接入weex的最佳方式)
weex-自定义module,实现weex在iOS的本地化,js之间互相跳转,交互,传值(iOS接入weex的最佳方式)
68 0
iOS开发-本地推送实现方法和数据处理方案(二)
iOS开发-本地推送实现方法和数据处理方案(二)
45 0
iOS开发-本地推送实现方法和数据处理方案(一)
iOS开发-本地推送实现方法和数据处理方案(一)
54 0
iOS开发 - 不通过import引入类名实现push或present
iOS开发 - 不通过import引入类名实现push或present
15 0
iOS开发 - 商品详情页两种分页模式,只提供思路和实现方式。
iOS开发 - 商品详情页两种分页模式,只提供思路和实现方式。
110 0
iOS开发 - 继udid,Mac地址等一系列唯一标识无效后,如何用KeyChain来实现设备唯一性
iOS开发 - 继udid,Mac地址等一系列唯一标识无效后,如何用KeyChain来实现设备唯一性
72 0
iOS开发 - swift通过Alamofire实现https通信
iOS开发 - swift通过Alamofire实现https通信
55 0
IOS中调用系统拨打电话与发送短信
IOS中调用系统拨打电话与发送短信
71 0
iOS漏洞:发送短信即可令任意苹果手机重启
国外网站上疯传一个苹果iOS手机操作系统的漏洞,给任何iPhone用户发送一个特定的由英文和阿拉伯文组成的字符串,即可令对方的手机重启。 此漏洞与苹果的Message应用和通知系统有关。一开始似乎只适用于iPhone用户之间的通讯,之后又有消息说安卓用户也能利用这个漏洞让iPhone重启。
1877 0
iOS-静默方式发送邮件(SKPSMTPMessage)
网上有很多关于这个库的文章,但很多都是直接抄DEMO里的代码,这个文章对如何使用进行了详细的说明,我在xcode8.0下,亲测通过,希望对其他有需要的同学有帮助
3163 0
文章
问答
文章排行榜
最热
最新
相关电子书
更多
Facebook iOS App技术演化十年之路
立即下载
From Java_Android to Swift iOS
立即下载
深入剖析 iOS 性能优化
立即下载