#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];
}