发短信"/>
iOS利用系统打电话和发短信
1.在app中直接调用两行代码就可以实现打电话的功能了,代码如下:
<pre name="code" class="objc">// 打电话
NSString *string = @"tel:1008611";// 要打的电话号码
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]];
2.发短信有两种方式,
(1)一种是跳出app调到发短信的系统页,(即,发完短信不回自动返回app,需重新打开app),方法简单,但不实用。和打电话一样实现方法,代码如下:
<pre name="code" class="objc">// app 外发短信
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"sms:1008611"]];
(2)在app中利用系统框架 MessageUI.framework 和
MFMessageComposeViewControllerDelegate 代理实现(有点复杂,但最实用)
步骤一:在Build Phases 的 Link Binary With Libraries 中导入系统框架 MessageUI.framework,在需要发短信的ViewController.m(视图控制器)中导入头文件
#import <MessageUI/MessageUI.h>
导入代理 (MFMessageComposeViewControllerDelegate)@interface ViewController ()<MFMessageComposeViewControllerDelegate>
步骤二:实现的代码如下
#pragma mark 发信息的触发方法
- (void)sendMessage
{if ([MFMessageComposeViewController canSendText])// 判断设备能不能发信息{UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"温馨提示:" message:@"发信息给对方" preferredStyle:UIAlertControllerStyleAlert];UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {// message 全局变量MFMessageComposeViewController *message = [[MFMessageComposeViewController alloc]init];message.messageComposeDelegate = self;message.body = @"我在测试,车友助理";message.recipients = [NSArray arrayWithObjects:@"1008611", @"1008611", nil];[self presentViewController:message animated:NO completion:nil];}];[alertC addAction:cancel];[alertC addAction:defaultAction];[self presentViewController:alertC animated:NO completion:nil];}else{UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"温馨提示:" message:@"当前设备不支持发信息功能" preferredStyle:UIAlertControllerStyleAlert];UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"默认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {}];[alertC addAction:cancel];[alertC addAction:defaultAction];[self presentViewController:alertC animated:NO completion:nil];}
}
#pragma mark 发信息的代理方法
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{switch (result){case MessageComposeResultCancelled:{[controller dismissViewControllerAnimated:NO completion:nil];UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"温馨提示:" message:@"取消发信息" preferredStyle:UIAlertControllerStyleAlert];UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];[alertC addAction:cancel];[self presentViewController:alertC animated:NO completion:nil];}break;case MessageComposeResultSent:{[controller dismissViewControllerAnimated:NO completion:nil];UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"温馨提示:" message:@"发信息成功!" preferredStyle:UIAlertControllerStyleAlert];UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];[alertC addAction:cancel];[self presentViewController:alertC animated:NO completion:nil];}break;case MessageComposeResultFailed:{[controller dismissViewControllerAnimated:NO completion:nil];UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"温馨提示:" message:@"发信息失败!" preferredStyle:UIAlertControllerStyleAlert];UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];[alertC addAction:cancel];}break;default:break;}
}
由于时间关系,没有对 UIAlertController 进行封装,看起来代码有些多,请见谅。转载请保障原著特权
更多推荐
iOS利用系统打电话和发短信
发布评论