打电话、发短信、web以及发邮件
#import "ViewController.h"
#import <MessageUI/MessageUI.h> //导入信息UI库 @interface ViewController () <MFMessageComposeViewControllerDelegate,MFMailComposeViewControllerDelegate> @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (IBAction)callPhone:(id)sender {
//方式1 :拼接字符串 注意开头是tel: 这种方式打电话回不到原来应用中,会停留在通讯录里,而且是直接拨打电话 没有任何弹窗提示
// NSString *str = [NSString stringWithFormat:@"tel:%@",self.phoneTextField.text];
// //首先得到应用的单例对象 然后调用openURL:这个方法 参数是url对象
// [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]]; //方式2,这种方式有弹窗提示,并且能回到原来应用中 推荐这种
// NSString *str1 = [[NSString alloc] initWithFormat:@"tel:%@",self.phoneTextField.text];
// //创建UIWebView对象
// UIWebView *callWebView = [[UIWebView alloc] init];
// //加载一个请求对象 这个请求对象通过url对象创建 url对象又通过str1字符串获得
// [callWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str1]]];
// //加入到self.view上
// [self.view addSubview:callWebView]; //方式3
//这种方式 也可以有弹窗提示 并且也能回到原来的应用中 也推荐这种
NSString *str2 = [NSString stringWithFormat:@"telprompt:%@",self.phoneTextField.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str2]]; } - (IBAction)callWeb:(id)sender { //打开网址 注意:打开的网址注意是http:// 或是https://
NSString *str = [NSString stringWithFormat:@"https://%@",self.webTextField.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]]; }
- (IBAction)sendSMS:(id)sender { //方式1:这种方式没法回到应用中,但是注意不要写中文等特殊字符 否则无法跳到发短信界面
//优点:简单 缺点:不能指定发送内容,只能指定发送人,而且不能回到应用中
// NSString *str = [NSString stringWithFormat:@"sms://%@",self.smsTextField.text];
// NSURL *url = [NSURL URLWithString:str];
// [[UIApplication sharedApplication] openURL:url]; //方式2 推荐这种
/*
优点:1.从应用出来并且能回到应用中
2.可以发送多人
3.可以用代码自定义消息
4.如果手机开通了iMessage功能,会走网络通道,不走运营商通道
*/ //判断用户设备是否能发送短信
if (![MFMessageComposeViewController canSendText]) {
NSLog(@"不能发送内容"); return ;
} //1.创建一个短信控制器对象
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init]; //2.设置短信内容
// (1)收件人
controller.recipients = @[@"",@""];
// (2)短信内容
controller.body = @"你好啊 你俩";
// (3)设置短信代理
controller.messageComposeDelegate = self; //3.显示短信控制器 [self presentViewController:controller animated:YES completion:^{
NSLog(@"显示短信控制器完成代码块");
}]; } #pragma mark - 短信控制器代理方法 - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result { /*
MessageComposeResultCancelled, 取消
MessageComposeResultSent, 发送
MessageComposeResultFailed 失败 result枚举
*/
NSLog(@"%d",result); //注:别忘了回到应用中
[controller dismissViewControllerAnimated:YES completion:^{
NSLog(@"短信控制器消失完成后代码块");
}]; } - (IBAction)sendEmail:(id)sender { //方式1
// NSString *str = [NSString stringWithFormat:@"mailto://%@",self.emailTextField.text];
// [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]]; //方式2
//判断是否能发送邮件
if (![MFMailComposeViewController canSendMail]) {
NSLog(@"不能发送邮件");
return;
}
//创建mail控制器对象
MFMailComposeViewController *vc = [[MFMailComposeViewController alloc] init];
//设置邮件主题
[vc setSubject:@"我爱你"];
//设置邮件发送内容 第二个参数支持HTML格式
[vc setMessageBody:@"基本的电话、邮件、短信" isHTML:YES];
//设置收件人列表
[vc setToRecipients:@[@"*******@qq.com"]];
//设置抄送人列表
[vc setCcRecipients:@[@"********@qq.com",@"********@163.com"]];
//设置邮件代理
vc.mailComposeDelegate = self;
//显示邮件控制器
[self presentViewController:vc animated:YES completion:^{
NSLog(@"跳转完成后执行代码块");
}]; } - (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error { /*
result枚举类型
MFMailComposeResultCancelled, 取消
MFMailComposeResultSaved, 保存
MFMailComposeResultSent, 发送
MFMailComposeResultFailed 失败
*/
NSLog(@"%d",result); [controller dismissViewControllerAnimated:YES completion:^{
NSLog(@"邮箱控制器消失完成后代码块");
}]; }
@end
打电话、发短信、web以及发邮件
另外补充一下,用下面的个人感觉更好一些!上面的UIWebView好像弹框的速度有点慢 自己弹框一下更方便一些
- (void)callAction:(NSString *)phone{
NSMutableString* str1=[[NSMutableString alloc]initWithString:phone];//存在堆区,可变字符串
[str1 insertString:@"-"atIndex:3];//把一个字符串插入另一个字符串中的某一个位置
[str1 insertString:@"-"atIndex:8];//把一个字符串插入另一个字符串中的某一个位置
UIAlertController *alert = [UIAlertController alertControllerWithTitle:str1 message:nil preferredStyle:UIAlertControllerStyleAlert];
// 设置popover指向的item
alert.popoverPresentationController.barButtonItem = self.navigationItem.leftBarButtonItem;
// 添加按钮
[alert addAction:[UIAlertAction actionWithTitle:@"呼叫" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
NSLog(@"点击了呼叫按钮");
NSString* phoneStr = [NSString stringWithFormat:@"tel://%@",phone];
if ([phoneStr hasPrefix:@"sms:"] || [phoneStr hasPrefix:@"tel:"]) {
UIApplication * app = [UIApplication sharedApplication];
if ([app canOpenURL:[NSURL URLWithString:phoneStr]]) {
[app openURL:[NSURL URLWithString:phoneStr]];
}
}
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"点击了取消按钮");
}]];
[self presentViewController:alert animated:YES completion:nil];
}
打电话、发短信、web以及发邮件的更多相关文章
- 打电话,发短信,发邮件,app跳转
1.打电话 - (IBAction)callPhone1:(id)sender { NSURL *url = [NSURL URLWithString:@"tel://18500441739 ...
- iOS中如何切换到发短信、打电话、发邮件
我们在做APP的时候,难免会遇到需要调用短信,电话等程序的时候.如美团. 当然,这些都只是一些简单的方法就可以实现,但是时间久了也会淡忘,所以想写这边博客.一是为了再捡起来复习一下,另一个相当于留个备 ...
- ios 设置亮度、声音;调用发短信、邮件、打电话
一,设置亮度 [[UIScreen mainScreen] setBrightness:0.5];//0.0~1.0 二,设置声音 1,添加 MediaPlayer.framework 框架 2,在需 ...
- iOS 打电话、发短信、邮件、打开网址、调用应用等合集
iOS中的很多功能都是非常简单的,几行代码就搞定了,比如打电话.打开网址.发邮件.发短信等,这里总结几个比较常用的: 1.打电话 方式一:最简单最直接的方式:直接跳到拨号界面 NSURL *url = ...
- html页面通过特殊链接:打电话,发短信,发邮件详细教程
采用url href链接的方式,实现在Safari ios,Android 浏览器,webos浏览器,塞班浏览器,IE,Operamini等主流浏览器,进行拨打电话功能. 1. 拨打电话 在电话号码 ...
- 移动设备wap手机网页html5通过特殊链接:打电话,发短信,发邮件详细教程
如果需要在移动浏览器中实现拨打电话,调用sms发短信,发送email等功能,移动手机WEB页面(HTML5)Javascript提供的接口是一个好办法. 采用url href链接的方式,实现在Safa ...
- IOS,发短信,发邮件,打电话
今天把APP里常用小功能 例如发短信.发邮件.打电话.全部拿出来简单说说它们的实现思路. 1.发短信实现打电话的功能,主要二种方法,下面我就分别说说它们的优缺点.1.1.发短信(1)——URL // ...
- Android实例-打电话、发短信和邮件,取得手机IMEI号(XE8+小米2)
结果: 1.不提示发短信卡住,点击没有反映,我猜想,可能是因为我用的是小米手机吧. 2.接收短信报错,我猜想可能是我改了里面的方法吧(哪位大神了解,求指教). 3.project -->opti ...
- WEB 移动网站 手机点击 打电话 发短信
原文地址: http://www.blesswe.com/portal.php?mod=view&aid=428 我们在手机浏览网页是希望用户看到手机号码点击就可以直接打电话或发短信,下面我们 ...
随机推荐
- noip 模拟赛 匹配 //贪婪策略
匹配(match.pas/match.c/match.cpp) [题目描述] 到了新的学期,Mcx痛苦的发现通用技术课居然是有实验课的,这样的话他就不得不放弃写作业的想法而去做一件类似于搭积木的事情. ...
- 采用Kettle分页处理大数据量抽取任务
作者:Grey 原文地址: http://greyzeng.com/2016/10/31/big-data-etl/ 需求: 将Oracle数据库中某张表历史数据导入MySQL的一张表里面. 源表(O ...
- 插入排序---直接插入排序算法(Javascript版)
将n个元素的数列分为已有序和无序两个部分. 数列:{a1,a2,a3,a4,…,an} 将该数列的第一元素视为有序数列,后面都视为无序数列: {{a1},{a2,a3,a4,…,an}} 将无序数列中 ...
- setTimeout,setInterval,process.nextTick,setImmediate in Nodejs
Nodejs的特点是事件驱动,异步I/O产生的高并发,产生此特点的引擎是事件循环,事件被分门别类地归到对应的事件观察者上,比如idle观察者,定时器观察者,I/O观察者等等,事件循环每次循环称为Tic ...
- 如何Windows分页控件中增加统计功能
在我的博客里面,很多Winform程序里面都用到了分页处理,这样可以不管是在直接访问数据库的场景还是使用网络方式访问WCF服务获取数据,都能获得较好的效率,因此WInform程序里面的分页控件的使用是 ...
- 硬链接 and 软链接
硬链接 软链接
- 【Java每日一题】20161201
20161130问题解析请点击今日问题下方的"[Java每日一题]20161201"查看 package Dec2016; public class Ques1201 { publ ...
- jQuery Mobile页面返回无需重新get
最近公司的web app项目,使得我有幸一直接触和学习jQuery Mobile.这确实是一个很不错的移动开发库,有助于擅长web开发的工程师,快速入门并构建自己的移动应用.但是在前两天,我碰到了一个 ...
- Monkey测试1——Monkey的使用
Monkey工具使用 一. 什么是Monkey Monkey是Android中的一个命令行工具,可以运行在模拟器里或实际设备中.它向系统发送伪随机的用户事件流(如按键输入.触摸屏输入.手势输入等),实 ...
- 网上图书商城3--Book模块
小技巧一:分页 ①PageBean<Book> findByCriteria(List<Expression> exprList, int pc) --- 通用的查询方法(p ...