美文网首页
OC与JS交互拨打电话功能

OC与JS交互拨打电话功能

作者: 纵昂 | 来源:发表于2019-08-16 09:24 被阅读0次

在开发项目中经常会使用到和Js交互的事情,我也是小菜鸟,以我遇到的项目截取的一小段demo与大家分享,这是OC调用Js的电话号码拨打电话功能,话不多说

#import "ViewController.h"
#import <WebKit/WebKit.h>
//1.HTML 要标记添加标记电话代码  2.WKWebView 调用 <WKNavigationDelegate> 代理
@interface ViewController ()<WKNavigationDelegate, WKUIDelegate,WKScriptMessageHandler>
@property (nonatomic, strong) WKWebView *wkWebView;

@end

在viewDidLoad去实现JS交互初始化

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    config.preferences.minimumFontSize = 50;
    
    self.wkWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) configuration:config];
    [self.view addSubview:self.wkWebView];
    self.wkWebView.UIDelegate = self;
    self.wkWebView.navigationDelegate = self;
    
//    https://m.benlai.com/huanan/zt/1231cherry
//    http://47.105.214.158:8081/h5/service.jsp
    NSURL *url = [NSURL URLWithString:@"http://47.105.214.158:8081/h5/service.jsp"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.wkWebView loadRequest:request];
    WKUserContentController *userCC = config.userContentController;
    [userCC addScriptMessageHandler:self name:@"showToast"]; //showMessage showToast
    
    
}

去调用和JS约定好的名称

#pragma mark - WKScriptMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
    NSLog(@"11---%@",NSStringFromSelector(_cmd));
    NSLog(@"message--%@",message);
    NSLog(@"message.body--%@",message.body);
    NSLog(@"message.name--%@",message.name);
    
    UIApplication *app = [UIApplication sharedApplication];
    //这个是注入JS代码后的处理效果,尽管html已经有实现了,但是没用,还是执行JS中的实现
    if ([message.name isEqualToString:@"showToast"]) {  //showMessage    showToast
        
        NSString *arrayStr = message.body;
        NSString *str = [NSString stringWithFormat:@"tel://%@",arrayStr]; //拨打电话
        
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
        
        //        [self showMsg:str];
    }

}

还有一种方法调用拨打电话的功能,和以上代码无关
/****************以下方法均没有用到*****************************************************/

#pragma mark - 3.实现代理方法   js页面拨打电话   处理拨打电话以及Url跳转等等    showToast  客服电话:400-090-8851
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    
    //    NSString *telNumber = [NSString stringWithFormat:@"tel:%@", @"88888888"];
    UIApplication *app = [UIApplication sharedApplication];
    NSURL *URL = navigationAction.request.URL;
    //        NSURL *URL = [NSURL URLWithString:telNumber];
    
    NSString *scheme = [URL scheme];
    if ([scheme isEqualToString:@"showToast"]) {
        if ([app canOpenURL:URL]) {
            CGFloat version = [[[UIDevice currentDevice]systemVersion]floatValue];
            if (version >= 10.0) {
                if (@available(iOS 10.0, *)) {
                    [[UIApplication sharedApplication]openURL:URL options:@{} completionHandler:nil];
                } else {
                    // Fallback on earlier versions
                }
            }else{
                
            }
        }
        NSString *resourceSpecifier = [URL resourceSpecifier];
        NSString *callPhone = [NSString stringWithFormat:@"tel://%@", resourceSpecifier];
        /// 防止iOS 10及其之后,拨打电话系统弹出框延迟出现
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone]];
        });
    }
    decisionHandler(WKNavigationActionPolicyAllow);
}

Demo地址:https://github.com/ZongAng123/OCandJS

相关文章

网友评论

      本文标题:OC与JS交互拨打电话功能

      本文链接:https://www.haomeiwen.com/subject/puoxsctx.html