我们在很多的App上面都会发现,你可以直接通过某个按钮拨打特定的电话号码,或者给某个人发送信息,本节就给大家讲解一下如何实现这些功能。
一、打电话
在程序内调用系统打电话,我们可以通过UIWebView实现
- 创建一个webView
- //通过UIWebView实现
- UIWebView *phoneWV = [[UIWebView alloc] initWithFrame:CGRectZero];
- [self.view addSubview:phoneWV];
- 读入电话号码
- //读入电话号码
- NSString *urlStr = [NSString stringWithFormat:@"tel://%@",self.myText.text];//self.myText.text是你要读入的电话号码,tel:不可省略,否则程序会出错
- NSURL *url = [NSURL URLWithString:urlStr];
- NSURLRequest *request = [NSURLRequest requestWithURL:url];
- 加载请求
- //加载请求
- [phoneWV loadRequest:request];
二、发短信
我们利用MFMessageComposeViewController,可以在不跳出程序的情况下发送我们的信息。
- 引入头文件,并添加代理接口
- #import <MessageUI/MessageUI.h>
- @interface ViewController ()<MFMessgeComposeViewControllerDelegate>
- @end
- 在设备支持的环境下,发送短信
- //如果设备支持发送短信,返回yes。
- if( [MFMessageComposeViewController canSendText] ){
- // MFMessageComposeViewController提供了操作界面,这里我们创建一个相应的控制器
- MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc]init];
- controller.recipients = [NSArray arrayWithObject:self.myText.text];
- controller.body = @"测试发短信";
- controller.messageComposeDelegate = self;
- //显示发送信息界面的控制器
- [self presentViewController:controller animated:YES completion:nil];
- }else{
- NSLog(@"设备不具备短信功能");
- }
- 通过实现相应的代理方法返回程序
- /*短信发送完成后返回app*/
- - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
- [controller dismissViewControllerAnimated:YES completion:nil];
- if (result == MessageComposeResultSent) {
- NSLog(@"发送成功");
- }
- }
三、发送邮件
与发送短信类似,我们需要利用MFMailComposeViewController,可以在不跳出程序的情况下发送我们的邮件。
- 引入头文件,并添加代理接口
- #import <MessageUI/MFMailComposeViewController.h>
- @interface ViewController ()<MFMailComposeViewControllerDelegate>
- @end
- 在设备支持的环境下,发送邮件
- //如果设备支持发送邮件,返回yes。
- if ([MFMailComposeViewController canSendMail]) {
- MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
- controller.mailComposeDelegate = self;
- [controller setToRecipients:[NSArray arrayWithObjects:self.myText.text, nil]];//self.myText.text为邮件地址
- //要发送的邮件主题
- [controller setSubject:@"邮件测试"];
- //要发送邮件的内容
- [controller setMessageBody:@"Hello " isHTML:NO];
- [self presentViewController:controller animated:YES completion:nil];
- }else{
- NSLog(@"设备不具备发送邮件功能");
- }
- 通过实现相应的代理方法返回程序
- - (void)mailComposeController:(MFMailComposeViewController*)controller
- didFinishWithResult:(MFMailComposeResult)result
- error:(NSError*)error;
- {
- if (result == MFMailComposeResultSent) {
- NSLog(@"邮件发送成功");
- }
- [self dismissViewControllerAnimated:YES completion:nil];
- }
四、示例代码
- 在故事板中加入三个按钮以及一个textField控件,并与viewController建立关联
- @property (weak, nonatomic) IBOutlet UITextField *myText;
- //打电话
- - (IBAction)clickBtn:(id)sender ;
- //发短信
- - (IBAction)clickSmsBtn:(id)sender;
- //发邮件
- - (IBAction)clickEmailBtn:(id)sender;
- 引入相应的头文件并添加代理接口
- #import <MessageUI/MessageUI.h>
- #import <MessageUI/MFMailComposeViewController.h>
- @interface ViewController ()<MFMessageComposeViewControllerDelegate,MFMailComposeViewControllerDelegate>
- @end
- 在相应按钮的点击方法中实现相关的功能,并实现相应的代理方法
- //打电话
- - (IBAction)clickBtn:(id)sender {
- //通过UIWebView实现
- UIWebView *phoneWV = [[UIWebView alloc] initWithFrame:CGRectZero];
- [self.view addSubview:phoneWV];
- //读入电话号码
- NSString *urlStr = [NSString stringWithFormat:@"tel:%@",self.myText.text];
- NSURL *url = [NSURL URLWithString:urlStr];
- NSURLRequest *request = [NSURLRequest requestWithURL:url];
- //加载请求
- [phoneWV loadRequest:request];
- }
- //发短信
- - (IBAction)clickSmsBtn:(id)sender {
- if( [MFMessageComposeViewController canSendText] ){
- MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc]init];;
- controller.recipients = [NSArray arrayWithObject:self.myText.text];
- controller.body = @"测试发短信";
- controller.messageComposeDelegate = self;
- [self presentViewController:controller animated:YES completion:nil];
- }else{
- NSLog(@"设备不具备短信功能");
- }
- }
- /*短信发送完成后返回app*/
- - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
- [controller dismissViewControllerAnimated:YES completion:nil];
- if (result == MessageComposeResultSent) {
- NSLog(@"发送成功");
- }
- }
- //发邮件
- - (IBAction)clickEmailBtn:(id)sender {
- if ([MFMailComposeViewController canSendMail]) {
- MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
- controller.mailComposeDelegate = self;
- [controller setToRecipients:[NSArray arrayWithObjects:self.myText.text, nil]];
- [controller setSubject:@"邮件测试"];
- [controller setMessageBody:@"Hello " isHTML:NO];
- [self presentViewController:controller animated:YES completion:nil];
- }else{
- NSLog(@"设备不具备发送邮件功能");
- }
- }
- - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
- {
- if (result == MFMailComposeResultSent) {
- NSLog(@"邮件发送成功");
- }
- [self dismissViewControllerAnimated:YES completion:nil];
- }