• iOS OC与JS的交互(JavaScriptCore实现)


    本文包括JS调用OC方法并传值,OC调用JS方法并传值

    本来想把html放进服务器里面,然后访问,但是觉得如果html在本地加载更有助于理解,特把html放进项目里

    HTML代码

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    </head>
    <body>
    <div style="margin-top: 20px">
    <h2>JS与OC交互</h2>
    <input type="button" value="唤起本地方法(call)" onclick="tianbai.call()">
    </div>
    <div>
    <input type="button" value="唤起getCall:(NSString *)callString传值" onclick="call()">
    </div>
    
    <script>
    
    
    var call = function()
    {
        var callInfo = JSON.stringify({"jianshu": "http://www.jianshu.com/users/55c8fdc3c6e7/latest_articles"});
            tianbai.getCall(callInfo);
    }
    
    var Callback = function(str)
    {
        alert(str);
    }
    var alerCallback = function()
    {
        alert('成功');
    }
    </script>
    </body>
    </html>

    上面html的代码:建立了两个button

    第一个button绑定了 tianbai.call() 方法,这里 tianbai 是一个对象,这个对象的作用下面OC代码中会说明, tianbai.call() 代表 tianbai 对象调用 call() 方法

    第二个button绑定了 call() 的方法,调用的是下面JavaScript中的 call() 方法,在 JavaScript 的 call() 里面,定义一个 callInfo 参数,方法中 tianbai.getCall(callInfo) 代表 tianbai 对象调用 getCall 方法并传参数 callInfo ,下面两个方法是OC调用JavaScript方法,其中Callback传回str,alerCallback为OC仅调用JavaScript方法!

    OC代码

    demo采用原生的JavaScriptCore类

    引入三个名词:

    1. JSContext:给JavaScript提供运行的上下文环境
    2. JSValue:JavaScript和Objective-C数据和方法的桥梁
    3. JSExport:这是一个协议,如果采用协议的方法交互,自己定义的协议必须遵守此协议

    ViewController.h中的代码

    //
    //  ViewController.h
    //  JavaScript
    //
    //  Created by tianbai on 16/6/8.
    //  Copyright © 2016年 厦门乙科网络公司. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    #import <JavaScriptCore/JavaScriptCore.h>
    
    @protocol JSObjcDelegate <JSExport>
    - (void)call; - (void)getCall:(NSString *)callString; @end

    @interface ViewController : UIViewController<UIWebViewDelegate, JSObjcDelegate>

    @property (nonatomic, strong) JSContext *jsContext; @property (strong, nonatomic) UIWebView *webView; @end

    ViewController.m中的代码

    //
    //  ViewController.m
    //  JavaScript
    //
    //  Created by tianbai on 16/6/8.
    //  Copyright © 2016年 厦门乙科网络公司. All rights reserved.
    //
    
    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
       
    }
    - (void)viewDidAppear:(BOOL)animated {
    self.webView
    = [[UIWebView alloc]initWithFrame:CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)]; self.webView.delegate = self; NSString* path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]; NSURL* url = [NSURL fileURLWithPath:path]; NSURLRequest* request = [NSURLRequest requestWithURL:url] ; [self.webView loadRequest:request]; [self.view addSubview:self.webView]; }
    - (void)webViewDidFinishLoad:(UIWebView *)webView {
    self.jsContext
    = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"]; self.jsContext[@"tianbai"] = self; self.jsContext.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
    context.exception
    = exceptionValue; NSLog(@"异常信息:%@", exceptionValue); }; } - (void)call {
    NSLog(
    @"call"); // 之后在回调js的方法Callback把内容传出去 JSValue *Callback = self.jsContext[@"Callback"]; //传值给web端 [Callback callWithArguments:@[@"唤起本地OC回调完成"]]; } - (void)getCall:(NSString *)callString {
    NSLog(
    @"Get:%@", callString); // 成功回调js的方法Callback JSValue *Callback = self.jsContext[@"alerCallback"]; [Callback callWithArguments:nil]; // 直接添加提示框 // NSString *str = @"alert('OC添加JS提示成功')"; // [self.jsContext evaluateScript:str]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
  • 相关阅读:
    SpringBoot中Word转PDF
    使用Word模板导出标准表Word样式文件
    Chrome 80及以上版本 中 Iframe 跨域 Cookie 的 Samesite 问题
    Visual Studio 2022 Key
    《HelloGitHub》第 69 期
    重玩 40 年前的经典游戏小蜜蜂,这次通关了源码
    那些年的开源项目,你跑起来了吗?
    图片处理看这篇就完了「GitHub 热点速览 v.21.48」
    年底巩固下 CS 知识「GitHub 热点速览 v.21.49」
    误入 GitHub 游戏区,意外地收获颇丰
  • 原文地址:https://www.cnblogs.com/xuzb/p/9183496.html
Copyright © 2020-2023  润新知