iOS拨打电话

模拟器在拨打电话方法不执行,必须真机才能拨打电话。一下方法是在iOS 8及以后系统下进行测试。Demo 下载

方法一、requestWithURL (推荐使用)

特性:
iOS 8:有弹框提示,点击确定,拨打电话(iOS 8.3下测试)
iOS 11:有弹框提示,点击确定,拨打电话(iOS 11.3.1下测试)
iOS 12:有弹框提示,点击确定,拨打电话(iOS 12.0下测试)
iOS 13 :有弹框提示,点击呼叫,拨打电话(iOS 13.0下测试)

OC代码:

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

Swift代码:

let callWebview =   UIWebView()
callWebview.loadRequest(NSURLRequest(url: URL(string: "tel:10086")!) as URLRequest)
self.view.addSubview(callWebview)

方法二、openURL(telprompt)

特性:
iOS 8:有弹框提示,点击确定,拨打电话(iOS 8.3下测试)
iOS 11:有弹框提示,点击确定,拨打电话(iOS 11.3.1下测试)
iOS 12:有弹框提示,点击确定,拨打电话(iOS 12.0下测试)
iOS 13 :有弹框提示,点击呼叫,拨打电话(iOS 13.0下测试)

网上说这个方法可能不合法,无法通过审核。

OC代码:

NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"telprompt:%@",@"10086"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];

Swift代码:

UIApplication.shared.openURL(URL(string: "telprompt:10086")!)

方法三、利用openURL(tel)

特性:
iOS 8:未弹框提示,直接拨打电话(iOS 8.3下测试)
iOS 11:有弹框提示,点击确定,拨打电话(iOS 11.3.1下测试)
iOS 12:有弹框提示,点击确定,拨打电话(iOS 12.0下测试)
iOS 13 :有弹框提示,点击呼叫,拨打电话(iOS 13.0下测试)

OC代码:

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

Swift代码:

UIApplication.shared.openURL(URL(string: "tel:10086")!)

Xcode8开发工具下使用- (BOOL)openURL:(NSURL*)url 会有如下警告!

Please use openURL:options:completionHandler: instead iOS10 API

改动:

- (BOOL)openURL:(NSURL*)url;//不提倡使用

iOS10之后建议改用下边的API替换

- (void)openURL:(NSURL*)url options:(NSDictionary *)options completionHandler:(void (^ __nullable)(BOOL success))completion;

备注:

  1. 在iOS10之后再用openURL: 的方法拨打电话会有1-2秒的延迟时间,iOS10之后使用openURL: options: completionHandler:的API可以解决延迟问题。

  2. openURL: options: completionHandler:方法API在iOS11下测试情况:拨打前弹出提示, and,拨打完以后会回到原来的应用。

例如

OC代码:

    NSString *callPhone = [NSString stringWithFormat:@"telprompt://%@",@"10086"];
    CGFloat version = [[[UIDevice currentDevice]systemVersion]floatValue];
      if (version >= 10.0) {
                /// 大于等于10.0系统使用此openURL方法
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone] options:@{} completionHandler:nil];
       } else {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone]];
       }

OC代码中,判断系统方法可以换成下边的语句(其实是Xcode9自动提示添加),这样的话,就不用自己取系统版本号了,而是用了系统方法@available(iOS 10.0, *),代码如下:

NSString *callPhone = [NSString stringWithFormat:@"telprompt://%@", @"10086"];
  if (@available(iOS 10.0, *)) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone] options:@{} completionHandler:nil];
   } else {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone]];
   }

Swift3 代码:

    let  tel = "10086"
    if #available(iOS 10.0, *) {
            UIApplication.shared.open(URL(string: "tel:" + tel!)!, options: [:], completionHandler: nil)
        } else {
            
             UIApplication.shared.openURL(URL(string: "tel:" + tel!)!)
        }

如果你觉得本文对你有帮助,请在下方点个 喜欢 ,让我知道这文章起了它该起的作用!谢谢!

下载:CallPhoneDemo

参考帖子:
http://www.th7.cn/Program/IOS/201406/216863.shtml
http://www.cocoachina.com/bbs/read.php?tid=177141

你可能感兴趣的:(iOS拨打电话)