在iOS开发过程中,经常会用到点击公司简介的电话号码,会直接拨打电话;或者点击网址链接发邮件,点击发短信按钮直接发短信等,这些功能虽小,但很实用。
一、拨打电话功能:
通过app拨打电话有三种方式,具体代码如下所示:
1.直接跳转到拨打电话界面,不弹出拨打电话提示框,打完电话不会返回到原app中:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://10086"]];
或者:
NSURL *url = [NSURL URLWithString:@"tel://10086"];
[[UIApplication sharedApplication] openURL:url];
2.拨打电话之前会弹出一个提示框询问用户是否拨号,拨打电话之后能自动返回到原app中:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"telprompt:10086"]];
或者:
NSURL *url = [NSURL URLWithString:@"telprompt://10086"];
[[UIApplication sharedApplication] openURL:url];
3.有弹出提示框,也会返回到原app中:
NSMutableString *str = [[NSMutableString alloc] initWithFormat:@"tel:%@",@"10086"];
UIWebView *callView = [[UIWebView alloc] init];
[callView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
[self.view addSubview:callView];
二、发短信功能:
1.不可以自定义短信内容,直接到发短信界面,发送完短信完成后会停留在发短信界面中,不能自动返回,需要手动返回原app中:
NSURL *url = [NSURL URLWithString:@"sms://10086"];
[[UIApplication sharedApplication]openURL:url];
2.可以自定义短信,也可以发送完短信直接返回原app中:
首先需要在项目里面导入一个发短信的框架:MessageUI.framework,如图所示:
然后在发短信调用的地方,导入头文件#import <MessageUI/MessageUI.h>,并且要遵循代理MFMessageComposeViewControllerDelegate,并实现代理方法。
//代理方法:
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
[self dismissViewControllerAnimated:YES completion:nil];
switch (result) {
case MessageComposeResultSent:
//短信发送成功
break;
case MessageComposeResultFailed:
//短信发送失败
break;
case MessageComposeResultCancelled:
//短信被用户取消发送
break;
default:
break;
}
}
//发送短信方法:
- (void)showMessageView:(NSArray *)phones title:(NSString *)title body:(NSString *)body {
if( [MFMessageComposeViewController canSendText]) {
MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc] init];
controller.recipients = phones;
controller.navigationBar.tintColor = [UIColor redColor];
controller.body = body;
controller.messageComposeDelegate = self;
[self presentViewController:controller animated:YES completion:nil];
[[[[controller viewControllers] lastObject] navigationItem] setTitle:title];//修改短信界面的标题
} else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息"
message:@"该设备不支持发送短信功能"
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil, nil];
[alert show];
}
}
//在需要发短信的具体地方,调用发送短信的方法:
[self showMessageView:[NSArray arrayWithObjects:@"10010",@"10086", nil] title:@"test" body:@"Hello world!"];
三、发送邮件
发送邮件的方法和发送短信的第二种方法差不多,第一步都是在项目里面导入一个发短信的框架:MessageUI.framework,然后在发邮件调用的地方,导入头文件#import <MessageUI/MessageUI.h>,并且要遵循代理MFMailComposeViewControllerDelegate,并实现代理方法。
//代理方法:
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
switch (result) {
case MFMailComposeResultCancelled : //取消发送mail
break;
case MFMailComposeResultSaved : //保存邮件
break;
case MFMailComposeResultSent : //发送邮件
break;
case MFMailComposeResultFailed : // 尝试保存或发送邮件失败
break;
default:
break;
}
//关闭邮件发送视图
[self dismissModalViewControllerAnimated:YES];
}
//发送邮件的方法:
- (void)sendMail {
MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
//设置委托
mail.mailComposeDelegate = self;
//设置邮件的主题
[mail setSubject:@"CSDN技术分享"];
//设置邮件的内容(两种格式:一种是html,一种是纯文本)
[mail setMessageBody:@"技术交流,多多指教" isHTML:NO];
//设置收件人
NSArray *toRecipients = [NSArray arrayWithObjects:@"912031168@qq.com",nil];
[mail setToRecipients:toRecipients];
//设置邮件附件为图片
NSString *path = [[NSBundle mainBundle] pathForResource:@"one" ofType:@"jpg"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[mail addAttachmentData:myData mimeType:@"image/png" fileName:@"man"];
//设置邮件发送内容
NSString *emailBody = @"CSDN博客地址:http://blog.csdn.net/cc1991_/article/details/70158723";
[mail setMessageBody:emailBody isHTML:NO];
//邮件发送的模态窗口
if ([MFMailComposeViewController canSendMail]) {
[self presentModalViewController:mail animated:YES];
}
}
//在需要发邮件的具体地方,调用发送邮件的方法:
[self sendMail];