iOS利用系统打电话和发短信

编程入门 行业动态 更新时间:2024-10-26 04:32:39

iOS利用系统打电话和<a href=https://www.elefans.com/category/jswz/34/1744591.html style=发短信"/>

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利用系统打电话和发短信

本文发布于:2024-03-15 09:39:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1738601.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发短信   系统   iOS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!