标签:
在开发某些应用时,我们可能希望能够调用iOS系统内置的电话、短信、邮件、浏览器应用,或者直接调用安装的第三方应用,这个要怎么实现呢?
//打开不同的系统应用
- (void)openUrl:(NSURL *)url;
那怎么区分我是要打电话还是发短信等等呢?
之前我们学习网络的时候,是不是URL最前面使用的是http://
,使用本地文件是不是前面就变成file://
了,这就是URL的协议,我们就是控制URL的协议,来打开不同应用。
tel://
和tel:
:打电话,没有提示直接拨打telprompt://
和telprompt:
:打电话,拨打电话前有提示用户是否拨打电话sms://
和sms:
:发短信mailto://
和mailto:
:发邮件http://
和http:
:打开浏览器//打电话
- (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];
}
//发送短信
- (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];
}
//发送邮件
- (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];
}
//浏览网页
- (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
实现,不过要进行一些配置。
info.plist
文件,添加URL types
节点URL Schemas
以及应用A的唯一标识URL identifier
,如下图: 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;//是否打开
}
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
提供了有现成的短信和邮件的编辑界面,开发人员只需要通过编程的方式给短信和邮件控制器设置对应的参数即可。
在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
在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
标签:
原文地址:http://blog.csdn.net/liuting5521826/article/details/51163580