打电话、发短信、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 我们在手机浏览网页是希望用户看到手机号码点击就可以直接打电话或发短信,下面我们 ...
随机推荐
- 《Inside UE4》-0-开篇
<Inside UE4>-0-开篇 InsideUE4 前言 VR行业发展是越来越火热了,硬件设备上有HTC VIVE,Oculus rift,PS VR,各种魔镜:平台上有Steam ...
- ES6笔记(7)-- Promise异步编程
系列文章 -- ES6笔记系列 很久很久以前,在做Node.js聊天室,使用MongoDB数据服务的时候就遇到了多重回调嵌套导致代码混乱的问题. JS异步编程有利有弊,Promise的出现,改善了这一 ...
- 图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12674 Accepted: 5651 ...
- C#对图片文件的压缩、裁剪操作初探
在做项目时,对图片的处理,以前都采用在上传时,限制其大小的方式,这样带来诸多不便.毕竟网站运维人员不一定会对图片做处理,经常超出大小限制,即使会使用图片处理软件的,也由于个人水平方面原因,处理效果差强 ...
- C#:Func的同步、异步调用
using System; namespace ActionDemo { class Program { static void Main(string[] args) { Console.Write ...
- RAID一个硬盘FAIL。
周六本想清静学习一下,刚把咖啡冲好还没有来得及坐下,机房却传来让人心揪的报警声,原来一台服务器一个硬盘FAIL(挂了...... 抽换好的一个容量大小的SCSI硬盘,再次进入这个介面,选择Force ...
- 【vbs】vbs写ini文件
这两天在折腾给一个项目打安装包,第一次接触软件打包,用的Advanced Installer(以下简称AI),应该说如果安装过程没有特殊动作(常规动作指释放文件.写注册表.建快捷方式等)的话,倒挺傻瓜 ...
- c# datetime 格式化
//c datetime 格式化 DateTime dt = DateTime.Now; Label1.Text = dt.ToString();//2005-11-5 13:21:25 Label2 ...
- HTTPS能有效保护用户隐私
HTTPS就等于HTTP加上TLS(SSL),HTTPS协议的目标主要有三个: http://hovertree.com/menu/webfront/ 数据保密性.保证内容在传输过程中不会被第三方查看 ...
- JSP动作元素
JSP动作元素分类 <jsp:include page="content.jsp"></jsp:include> 使用<%@ include%> ...