iOS学习笔记28-系统服务(一)短信和邮件
2016-11-16 13:21
483 查看
一、系统应用
在开发某些应用时,我们可能希望能够调用iOS系统内置的电话、短信、邮件、浏览器应用,或者直接调用安装的第三方应用,这个要怎么实现呢?这里统一使用UIApplication的一个对象方法来实现:
//打开不同的系统应用 - (void)openUrl:(NSURL *)url;
那怎么区分我是要打电话还是发短信等等呢?
之前我们学习网络的时候,是不是URL最前面使用的是
http://,使用本地文件是不是前面就变成
file://了,这就是URL的协议,我们就是控制URL的协议,来打开不同应用。
下面列出了一些系统应用URL协议:
tel://和
tel::打电话,没有提示直接拨打
telprompt://和
telprompt::打电话,拨打电话前有提示用户是否拨打电话
sms://和
sms::发短信
mailto://和
mailto::发邮件
http://和
http::打开浏览器
下面就是具体实例演示:
1. 有提示的打电话
//打电话 - (void)telpromptTest{ //电话号码 NSString *phoneNumber = @"18500138888"; //1.创建打电话URL路径,这种方式会提示用户确认是否拨打电话 NSString *urlStr = [NSString stringWithFormat:@"telprompt://%@",phoneNumber]; //2.生成URL NSURL *url = [NSURL URLWithString:urlStr]; //3.打开系统应用 UIApplication *application = [UIApplication sharedApplication]; [application openURL:url]; }
2. 发短信
//发送短信 - (void)sendMessageTest{ //电话号码 NSString *phoneNumber = @"18500138888"; //1.创建发短信URL路径 NSString *urlStr = [NSString stringWithFormat:@"sms://%@",phoneNumber]; //2.生成URL NSURL *url = [NSURL URLWithString:urlStr]; //3.打开系统应用 UIApplication *application = [UIApplication sharedApplication]; [application openURL:url]; }
3. 发邮件
//发送邮件 - (void)sendEmailTest { NSString *mailAddress = @"850192964@qq.com"; //1.创建发邮件URL路径 NSString *urlStr = [NSString stringWithFormat:@"mailto://%@",mailAddress]; //2.生成URL NSURL *url = [NSURL URLWithString:urlStr]; //3.打开系统应用 UIApplication *application = [UIApplication sharedApplication]; [application openURL:url]; }
4. 打开浏览器
//浏览网页 - (void)browserTest { //1.创建打开浏览器URL路径 NSString *urlStr = @"http://www.baidu.com"; //2.生成URL NSURL *url = [NSURL URLWithString:urlStr]; //3.打开系统应用 UIApplication *application = [UIApplication sharedApplication]; [application openURL:url]; }
上面打开的是系统应用,实际上
openUrl的功能是只要是系统安装了的应用程序,都可以打开,比如假设你现在开发了一个应用A,如果用户机器上已经安装了此应用,并且在应用B中希望能够直接打开A,也是可以用
openUrl实现,不过要进行一些配置。
配置第三方应用步骤:
修改应用A的
info.plist文件,添加
URL types节点
在该节点下,配置具体协议
URL Schemas以及应用A的唯一标识
URL identifier,如下图:
在应用A的
AppDelegate文件中处理
AppDelegate的一个代理方法
/* 当被其他应用程序通过URL打开时就会调用, 这里可以接收参数并解析,返回是否能被其他应用程序打开 */ -(BOOL)application:(UIApplication *)application //当前应用程序 openURL:(NSURL *)url //其他应用使用的URL sourceApplication:(NSString *)sourceApplication //其他应用的应用标识 annotation:(id)annotation { NSLog(@"url:%@",url); NSLog(@"source:%@",sourceApplication); NSLog(@"params:%@",[url host]); return YES;//是否打开 }
配置应用A完成,然后我们就可以在应用B中使用
openUrl打开应用A了
//打开第三方应用 - (void)thirdPartyApplicationTest { //使用第三方应用协议 NSString *urlStr = @"cmj://myparams"; NSURL *url = [NSURL URLWithString:urlStr]; //判断该应用是否能打开 UIApplication *application = [UIApplication sharedApplication]; if(![application canOpenURL:url]){ NSLog(@"无法打开\"%@\",请确保此应用已经正确安装.",url); return; } [application openURL:url]; }
二、系统服务
调用系统内置的应用来发送短信、邮件相当简单,但是这么操作也存在着一些弊端:当你点击了发送短信(或邮件)操作之后,直接启动了系统的短信(或邮件)应用程序,我们的应用其实此时已经处于一种挂起状态,发送完(短信或邮件)之后无法自动回到应用界面。
如果想要在应用程序内部完成这些操作,则可以利用iOS中的
MessageUI.framework,它提供了关于短信和邮件的UI接口供开发者在应用程序内部调用。
MessageUI.framework提供了有现成的短信和邮件的编辑界面,开发人员只需要通过编程的方式给短信和邮件控制器设置对应的参数即可。
1、短信
在MessageUI.framework中使用
MFMessageComposeViewController来发短信
下面是使用步骤:
导入
MessageUI.framework,并添加头文件
#import <MessageUI/MessageUI.h>
创建
MFMessageComposeViewController对象
设置收件人、信息正文等内容属性,设置代理
messageComposeDelegate
以模态弹出该视图控制器
处理代理方法,获取发送状态:
/* 发送完成会调用,不管成功与否 */ -(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result;/*发送结果*/
下面是使用实例:
#import "KCSendMessageViewController.h" #import <MessageUI/MessageUI.h> @interface KCSendMessageViewController ()<MFMessageComposeViewControllerDelegate> @property (weak, nonatomic) IBOutlet UITextField *receivers;//收信人文本框 @property (weak, nonatomic) IBOutlet UITextField *body;//信息正文文本框 @property (weak, nonatomic) IBOutlet UITextField *subject;//主题文本框 @property (weak, nonatomic) IBOutlet UITextField *attachments;//附件文本框 @end @implementation KCSendMessageViewController #pragma mark - 控制器视图方法 - (void)viewDidLoad { [super viewDidLoad]; } #pragma mark - UI事件 - (IBAction)sendMessageClick:(UIButton *)sender { //如果不能发送文本信息,就直接返回 if(![MFMessageComposeViewController canSendText]){ return; } //创建短信发送视图控制器 MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init]; //设置收件人 messageController.recipients = [self.receivers.text componentsSeparatedByString:@","]; //设置信息正文 messageController.body = self.body.text; //设置代理,注意这里不是delegate而是messageComposeDelegate messageController.messageComposeDelegate = self; //判断是否支持主题 if([MFMessageComposeViewController canSendSubject]){ //设置主题 messageController.subject = self.subject.text; } //判断是否支持附件 if ([MFMessageComposeViewController canSendAttachments]) { //添加附件,请务必指定附件文件的后缀,否则在发送后无法正确识别文件类别 NSArray *attachments = [self.attachments.text componentsSeparatedByString:@","]; if (attachments.count > 0) { for(NSString *attachment in attachments){ NSString *path = [[NSBundle mainBundle] pathForResource:attachment ofType:nil]; NSURL *url = [NSURL fileURLWithPath:path]; //添加附件具体方法,需要设置附件URL和附件的标识 [messageController addAttachmentURL:url withAlternateFilename:attachment]; }; } } //以模态弹出界面 [self presentViewController:messageController animated:YES completion:nil]; } #pragma mark - MFMessageComposeViewController代理方法 /* 发送完成,不管成功与否 */ -(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result { switch (result) { case MessageComposeResultSent: NSLog(@"发送成功."); break; case MessageComposeResultCancelled: NSLog(@"取消发送."); break; default: NSLog(@"发送失败."); break; } //弹回界面 [self dismissViewControllerAnimated:YES completion:nil]; } @end
2. 邮件
在MessageUI.framework中使用
MFMailComposeViewController来发邮件
使用步骤【和发短信相似】:
导入
MessageUI.framework,并添加头文件
#import <MessageUI/MessageUI.h>
创建
MFMailComposeViewController对象
设置收件人、抄送人、正文等内容属性,设置代理
mailComposeDelegate
以模态弹出该视图控制器
处理代理方法,获取发送状态:
/* 发送完成会调用,不管成功与否 */ -(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result /* 发送结果 */ error:(NSError *)error;/* 错误信息 */
下面是实例:
#import "KCSendEmailViewController.h" #import <MessageUI/MessageUI.h> @interface KCSendEmailViewController ()<MFMailComposeViewControllerDelegate> @property (weak, nonatomic) IBOutlet UITextField *toTecipients;//收件人文本框 @property (weak, nonatomic) IBOutlet UITextField *ccRecipients;//抄送人文本框 @property (weak, nonatomic) IBOutlet UITextField *bccRecipients;//密送人文本框 @property (weak, nonatomic) IBOutlet UITextField *subject; //主题文本框 @property (weak, nonatomic) IBOutlet UITextField *body;//正文文本框 @property (weak, nonatomic) IBOutlet UITextField *attachments;//附件文本框 @end @implementation KCSendEmailViewController - (void)viewDidLoad { [super viewDidLoad]; } #pragma mark - UI事件 - (IBAction)sendEmailClick:(UIButton *)sender { //判断当前是否能够发送邮件 if ([MFMailComposeViewController canSendMail]) { return; } //创建发送邮件视图控制器 MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init]; //设置代理,注意这里不是delegate,而是mailComposeDelegate mailController.mailComposeDelegate = self; //设置收件人 NSArray *recipients = [self.toTecipients.text componentsSeparatedByString:@","]; [mailController setToRecipients:recipients]; //设置抄送人 if (self.ccRecipients.text.length > 0) { NSArray *ccRecipients = [self.ccRecipients.text componentsSeparatedByString:@","]; [mailController setCcRecipients:ccRecipients]; } //设置密送人 if (self.bccRecipients.text.length > 0) { NSArray *bccRecipients = [self.bccRecipients.text componentsSeparatedByString:@","]; [mailController setBccRecipients:bccRecipients]; } //设置主题 [mailController setSubject:self.subject.text]; //设置主体内容 [mailController setMessageBody:self.body.text isHTML:YES]; //添加附件 if (self.attachments.text.length > 0) { NSArray *attachments = [self.attachments.text componentsSeparatedByString:@","] ; for(NSString *attachment in attachments) { NSString *file = [[NSBundle mainBundle] pathForResource:attachment ofType:nil]; NSData *data = [NSData dataWithContentsOfFile:file]; //第一个参数是附件数据,第二个参数是mimeType类型,jpg图片对应image/jpeg [mailController addAttachmentData:data mimeType:@"image/jpeg" fileName:attachment]; }]; } //弹出视图 [self presentViewController:mailController animated:YES completion:nil]; } #pragma mark - MFMailComposeViewController代理方法 /* 发送完成会调用,不管成功与否 */ -(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error { switch (result) { case MFMailComposeResultSent: NSLog(@"发送成功."); break; case MFMailComposeResultSaved: //点取消会提示是否存储为草稿,存储后可以到系统邮件应用的对应草稿箱找到 NSLog(@"邮件已保存."); break; case MFMailComposeResultCancelled: NSLog(@"取消发送."); break; default: NSLog(@"发送失败."); break; } if (error) { NSLog(@"发送邮件过程中发生错误,错误信息:%@",error.localizedDescription); } [self dismissViewControllerAnimated:YES completion:nil]; } @end
相关文章推荐
- iOS学习笔记28-系统服务(一)短信和邮件
- iOS学习笔记28-系统服务(一)短信和邮件
- iOS学习笔记 系统服务(一)短信和邮件
- iOS学习笔记 系统服务(二)通讯录
- iOS学习笔记 系统服务(三)蓝牙
- iOS学习笔记29-系统服务(二)通讯录
- iOS学习笔记30-系统服务(三)蓝牙
- iOS学习笔记29-系统服务(二)通讯录
- iOS学习笔记29-系统服务(二)通讯录
- iOS学习笔记30-系统服务(三)蓝牙
- iOS学习笔记30-系统服务(三)蓝牙
- iOS学习笔记8-iOS 实现发送邮件和短信
- iOS使用系统服务-短信与邮件
- android菜鸟学习笔记28----Android中的Service生命周期及本地和远程服务绑定的实现
- 黑马程序员-IOS学习笔记 OC创建对象时 系统做了些什么
- ios学习笔记--(c基础题8.万年历小系统)
- ios学习笔记-(oc基础-面向对象小系统)
- iOS学习笔记28-JS执行过程分析
- iOS学习笔记32-iOS 9系统策略更新,使用微信SDK的开发者注意升级
- IOS学习之——短信分享 邮件分享