zoukankan      html  css  js  c++  java
  • JS与OC交互--简单使用

    直接上代码

    .m文件

    #import "ViewController.h"
    
    @interface ViewController () <UIWebViewDelegate>
    @property (weak, nonatomic) IBOutlet UIWebView *webView;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad]; //通过本地html文件加载网页
        [self.webView loadRequest:[NSURLRequest 
        requestWithURL:[[NSBundle mainBundle]
         URLForResource:@"test" withExtension:@"html"]]];
    }
    - (void)call{    //拨打电话
        [[UIApplication sharedApplication]
        openURL:[NSURL URLWithString:@"tel://10086"]];
    }
    //是否允许加载从webview获得的请求/*
     *该方法可以实现js调用OC
     *js和OC交互的第三框架可以使用:WebViewJavaScriptBridge
     */
    
    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{  
       //获得html点击的链接
        NSString *url = request.URL.absoluteString;    //设置协议头
        NSString *scheme = @"zc://";    //判断获得的链接前面是否包含设置头
        if([url hasPrefix:scheme]){        //切割字符串
            NSString *methodName = 
       [url substringFromIndex:scheme.length];   
    //调用打电话的方法
    
    [self performSelector:NSSelectorFromString(methodName) withObject:nil];     
           return NO;
        }else{     
              return YES;
        }
    }

    html 文件

    storyboard

    运行结果

  • 相关阅读:
    简单的登录验证小程序_python
    远程执行命令_python
    远程执行本地脚本_linux
    反射_python
    ssh oa项目介绍
    返回上一级过程
    ssh框架开发crm(客户关系系统总结)
    struct相对路径,绝对路径
    <s:textfield>标签回显
    ssh框架整合
  • 原文地址:https://www.cnblogs.com/fengmin/p/5775482.html
Copyright © 2011-2022 走看看