首页 > 系统 > iOS > 正文

iOS 拨号与发送短信功能的实现

2019-11-09 18:15:39
字体:
来源:转载
供稿:网友

iOS 拨号与发送短信功能的实现

一.拨号功能

拨号功能有三种方法: 1.第一种:会弹出呼叫提示,拨打完电话后无法回到原来的应用,会停留在通讯录里

NSString* str=[@"tel:10086"];[[UIapplication sharedApplication] openURL:[NSURL URLWithString:str]];

2.第二种:会弹出呼叫提示,拨打完电话后会回到原来的应用,但是是私有API,不合法。

NSString* str=[@"telPRompt://10086"];[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];

3.第三种:拨打电话后还会回到原来的程序,也会弹出提示。

NSString* str=@"tel:10086"];UIWebView* callWebview = [[UIWebView alloc] init];[callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];[self.view addSubview:callWebview];

PS:在拨打电话过程中可以得到一些本机数据(服务商,移动业务国家代码,移动网络代码,所在国家,是否允许网络电话)。

CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];CTCarrier *carrier = info.subscriberCellularProvider;NSLog(@"carrier:%@", [carrier description]);

2.短信功能

短信功能有两种方法: 1.第一种

[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"sms://10086"]];

2.第二种 首先需要加入头文件

#import <MessageUI/MessageUI.h>

使用MessageUI 框架发送短信

-(void)sendMessage { //用于判断是否有发送短信的功能(模拟器上就没有短信功能) Class messageClass = (NSClassFromString(@"MFMessageComposeViewController")); //判断是否有短信功能 if (messageClass != nil) { MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init]; messageController.messageComposeDelegate = self; //拼接并设置短信内容 NSString *messageContent = [NSString stringWithFormat:@"蛤蛤蛤蛤蛤蛤"]; messageController.body = messageContent; //设置发送给谁 messageController.recipients = @[@"10086"]; //推到发送视图控制器 [self presentViewController:messageController animated:YES completion:^{ }]; } else { UIAlertController* alertView = [UIAlertController alertControllerWithTitle:@"提示" message:@"iOS版本过低" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {}]; [alertView addAction:cancelAction]; [self presentViewController:alertView animated:YES completion:nil]; }}

实现代理函数

-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result { switch (result) { case MessageComposeResultCancelled: NSLog(@"返回APP界面") ; break; case MessageComposeResultFailed: NSLog(@"发送失败") ; break; case MessageComposeResultSent: NSLog(@"发送成功") ; break; default: break; } //最后解除短息发送界面,返回原app [self dismissViewControllerAnimated:YES completion:NULL];}

最后设置一个按钮,点击即发送固定短信给固定号码

- (IBAction)sendMegAction:(UIButton *)sender { [self sendMessage];}

Github:https://github.com/NowOrNeverDG/DialingAndSendMeg


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表