iOS学习笔记28-系统服务(一)短信和邮件
发布日期:2022-03-12 04:49:26 浏览次数:32 分类:技术文章

本文共 8715 字,大约阅读时间需要 29 分钟。

 

一、系统应用

在开发某些应用时,我们可能希望能够调用iOS系统内置的电话、短信、邮件、浏览器应用,或者直接调用安装的第三方应用,这个要怎么实现呢?

这里统一使用UIApplication的一个对象方法来实现:
//打开不同的系统应用- (void)openUrl:(NSURL *)url;

那怎么区分我是要打电话还是发短信等等呢?

之前我们学习网络的时候,是不是URL最前面使用的是http://,使用本地文件是不是前面就变成file://了,这就是URL的协议,我们就是控制URL的协议,来打开不同应用。

下面列出了一些系统应用URL协议:
  1. tel://tel:打电话,没有提示直接拨打
  2. telprompt://telprompt:打电话,拨打电话前有提示用户是否拨打电话
  3. sms://sms:发短信
  4. mailto://mailto:发邮件
  5. 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实现,不过要进行一些配置。

配置第三方应用步骤:
  1. 修改应用A的info.plist文件,添加URL types节点
  2. 在该节点下,配置具体协议URL Schemas以及应用A的唯一标识URL identifier,如下图:

     

  3. 在应用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来发短信

下面是使用步骤:
  1. 导入MessageUI.framework,并添加头文件

    #import 
  2. 创建MFMessageComposeViewController对象
  3. 设置收件人、信息正文等内容属性,设置代理messageComposeDelegate
  4. 以模态弹出该视图控制器
  5. 处理代理方法,获取发送状态:

    /* 发送完成会调用,不管成功与否 */-(void)messageComposeViewController:(MFMessageComposeViewController *)controller                didFinishWithResult:(MessageComposeResult)result;/*发送结果*/
下面是使用实例:
#import "KCSendMessageViewController.h"#import 
@interface KCSendMessageViewController ()
@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来发邮件

使用步骤【和发短信相似】:
  1. 导入MessageUI.framework,并添加头文件

    #import 
  2. 创建MFMailComposeViewController对象
  3. 设置收件人、抄送人、正文等内容属性,设置代理mailComposeDelegate
  4. 以模态弹出该视图控制器
  5. 处理代理方法,获取发送状态:

    /* 发送完成会调用,不管成功与否 */-(void)mailComposeController:(MFMailComposeViewController *)controller         didFinishWithResult:(MFMailComposeResult)result /* 发送结果 */                      error:(NSError *)error;/* 错误信息 */
下面是实例:
#import "KCSendEmailViewController.h"#import 
@interface KCSendEmailViewController ()
@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

 

转载于:https://www.cnblogs.com/ming1025/p/6069154.html

转载地址:https://blog.csdn.net/weixin_30348519/article/details/98168699 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:hive输出json字符串
下一篇:UICollectionView入门--使用系统UICollectionViewFlowLayout布局类

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月08日 19时53分14秒