移动应用
7263
首先宏定义一个号码:
#define PhoneNumber @"18188888888" /** 宏定义一个号码 */
短信发送的两种方式:
方式一
使用系统底层的发送:
/** 底层发送方式 */ /** 缺点: 无法自定义短信内容,无法返回原来应用; */ NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"sms://%@",PhoneNumber]]; [[UIApplication sharedApplication] openURL:url];
方式二
使用第三方框架发送,需要导入MessageUI头文件,并遵守协议
#import<MessageUI/MessageUI.h> @interface TestViewController ()<MFMessageComposeViewControllerDelegate> /** 自定义一个发送短信的方法 */ - (void) sendMsgWithPhoneNumber:(NSArray *)phone title: (NSString *)title body: (NSString *)body { if ([MFMessageComposeViewController canSendText]) { NSLog(@"..."); MFMessageComposeViewController *msgComposeVC = [[MFMessageComposeViewController alloc] init]; msgComposeVC.recipients = phone; msgComposeVC.navigationBar.tintColor = [UIColor redColor]; msgComposeVC.body = body; msgComposeVC.messageComposeDelegate = self; msgComposeVC.title = title; [self presentViewController:msgComposeVC animated:YES completion:nil]; } else { UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"提示信息" message: @"该设备不支持短信发送" delegate: nil cancelButtonTitle: @"确定" otherButtonTitles: nil, nil]; [alert show]; } }
调用自定义的方法发送消息:
/** 第三方库发送方式,弥补了方式一的各种缺点 */ [self sendMsgWithPhoneNumber:[NSArray arrayWithObjects:PhoneNumber,nil] title:@"test" body:@"This is a test to send message"];
发短信第三方框架的协议:
#pragma mark -MFMessageComposeViewControllerDelegate - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result { [self dismissViewControllerAnimated:YES completion:nil]; switch (result) { case MessageComposeResultSent: /** 消息发送成功 */ break; case MessageComposeResultFailed: /** 消息发送失败 */ break; case MessageComposeResultCancelled: /** 用户取消发送消息 */ break; } }
拨打电话的三种方式:
/** 方式一: 底层最简单的拨打电话 */ /** 缺点:没有提醒,当方法触发后就直接拨打,无法返回上一个界面 */ - (void) callTestOne { NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",PhoneNumber]]; [[UIApplication sharedApplication] openURL:url]; } /** 方式二: 苹果私有协议 */ /** 有提醒,可以返回上一个界面,不过该方法上线会被拒绝,可以用于开发越狱软件 */ - (void) callTestTwo { NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"telprompt://%@",PhoneNumber]]; [[UIApplication sharedApplication] openURL: url]; } /** 方式三: 加载 web请求 */ /** 该方法是开发中最常用的方法,有提醒,可以返回上一个界面,就是代码多了点而已 */ - (void) callTestThree { NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",PhoneNumber]]; if (_webView == nil) { _webView = [[UIWebView alloc] initWithFrame:CGRectZero]; } NSURLRequest *request = [NSURLRequest requestWithURL:url]; [_webView loadRequest:request]; }