1 #import "ViewController.h"
2 #import <MessageUI/MessageUI.h> //导入信息UI库
3
4 @interface ViewController () <MFMessageComposeViewControllerDelegate,MFMailComposeViewControllerDelegate>
5
6 @end
7
8 @implementation ViewController
9
10 - (void)viewDidLoad {
11 [super viewDidLoad];
12 // Do any additional setup after loading the view, typically from a nib.
13 }
14
15 - (void)didReceiveMemoryWarning {
16 [super didReceiveMemoryWarning];
17 // Dispose of any resources that can be recreated.
18 }
19
20 - (IBAction)callPhone:(id)sender {
21 //方式1 :拼接字符串 注意开头是tel: 这种方式打电话回不到原来应用中,会停留在通讯录里,而且是直接拨打电话 没有任何弹窗提示
22 // NSString *str = [NSString stringWithFormat:@"tel:%@",self.phoneTextField.text];
23 // //首先得到应用的单例对象 然后调用openURL:这个方法 参数是url对象
24 // [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
25
26
27 //方式2,这种方式有弹窗提示,并且能回到原来应用中 推荐这种
28 // NSString *str1 = [[NSString alloc] initWithFormat:@"tel:%@",self.phoneTextField.text];
29 // //创建UIWebView对象
30 // UIWebView *callWebView = [[UIWebView alloc] init];
31 // //加载一个请求对象 这个请求对象通过url对象创建 url对象又通过str1字符串获得
32 // [callWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str1]]];
33 // //加入到self.view上
34 // [self.view addSubview:callWebView];
35
36
37 //方式3
38 //这种方式 也可以有弹窗提示 并且也能回到原来的应用中 也推荐这种
39 NSString *str2 = [NSString stringWithFormat:@"telprompt:%@",self.phoneTextField.text];
40 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str2]];
41
42 }
43
44 - (IBAction)callWeb:(id)sender {
45
46 //打开网址 注意:打开的网址注意是http:// 或是https://
47 NSString *str = [NSString stringWithFormat:@"https://%@",self.webTextField.text];
48 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
49
50
51 }
52 - (IBAction)sendSMS:(id)sender {
53
54 //方式1:这种方式没法回到应用中,但是注意不要写中文等特殊字符 否则无法跳到发短信界面
55 //优点:简单 缺点:不能指定发送内容,只能指定发送人,而且不能回到应用中
56 // NSString *str = [NSString stringWithFormat:@"sms://%@",self.smsTextField.text];
57 // NSURL *url = [NSURL URLWithString:str];
58 // [[UIApplication sharedApplication] openURL:url];
59
60
61 //方式2 推荐这种
62 /*
63 优点:1.从应用出来并且能回到应用中
64 2.可以发送多人
65 3.可以用代码自定义消息
66 4.如果手机开通了iMessage功能,会走网络通道,不走运营商通道
67 */
68
69 //判断用户设备是否能发送短信
70 if (![MFMessageComposeViewController canSendText]) {
71 NSLog(@"不能发送内容");
72
73 return ;
74 }
75
76 //1.创建一个短信控制器对象
77 MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
78
79 //2.设置短信内容
80 // (1)收件人
81 controller.recipients = @[@"10086",@"10010"];
82 // (2)短信内容
83 controller.body = @"你好啊 你俩";
84 // (3)设置短信代理
85 controller.messageComposeDelegate = self;
86
87 //3.显示短信控制器
88
89 [self presentViewController:controller animated:YES completion:^{
90 NSLog(@"显示短信控制器完成代码块");
91 }];
92
93
94 }
95
96 #pragma mark - 短信控制器代理方法
97
98 - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
99
100 /*
101 MessageComposeResultCancelled, 取消
102 MessageComposeResultSent, 发送
103 MessageComposeResultFailed 失败
104
1