iOS 拨打电话(解决openURL延迟和不同方法比较)

转载请注明出处!!!

iOS拨打电话有三种方法。

注意:最新的iOS12上测试 三种方法效果没有区别 也不要开线程

第一种:

NSMutableString *str=[[NSMutableString alloc] initWithFormat:@"tel:%@",@"186xxxx6979"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];

第二种:

NSMutableString *str2=[[NSMutableString alloc] initWithFormat:@"tel:%@",@"186xxxx6979"];
UIWebView * callWebview = [[UIWebView alloc] init];
[callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str2]]];
[self.view addSubview:callWebview];

第三种:

NSMutableString *str3=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",@"186xxxx6979"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str3]];

三种方法优缺点:

网上有解释为第一种打完电话留在打电话界面,第二种打完电话回到原来的app

真实测试:两种打完电话都是回到原来的app界面,

方法一:在iOS10.2之前没问题,没有提示直接拨打,但是在iOS10.2后新增弹出提示,弹出提示有延迟。修改方法有几种

1. 调用[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str] options:@{} completionHandler:nil];这个方法但是需要判断版本,在iOS10之后才用。10之前用原来的 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];

2.关于拨打电话的方法,自己凭喜好选择,导致弹出框延迟的原因,目前初步诊断就是openURL在iOS 10及其之后会阻塞主线程

NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",@"18511089439"];

    dispatch_async(dispatch_get_global_queue(0, 0), ^{

        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];

    });

方法二:点击有提示。但是照此方法写的会不停创建webview。应当声明一个web,每次都调用同一个以节省资源 

方法三:点击有提示。用这个的时候要小心,因为apple的文档里边没出现过telprompt这个。之前是有过被reject的案例。

总结:推荐第二种,都是在第一种延迟取消后,还是可以用第一种的

原文地址:https://www.cnblogs.com/weicyNo-1/p/7151605.html