iOS OC与JS的交互(JavaScriptCore实现)
2024-08-27 14:24:23
本文包括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类
引入三个名词:
- JSContext:给JavaScript提供运行的上下文环境
- JSValue:JavaScript和Objective-C数据和方法的桥梁
- 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(, , [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
最新文章
- Python Day18
- hdu 2896 字典树解法
- Android--多线程之Handler(转)
- Leetcode 235 Lowest Common Ancestor of a Binary Search Tree 二叉树
- UITabBar 设置字体的颜色(选中状态/正常状态)setTitleTextAttributes
- 【T-SQL系列】FOR XML PATH 语句的应用
- 从github上面拷贝源码
- JS 基于面向对象的 轮播图2
- 理解JavaScript的定时器与回调机制
- 64位 CentOS NDK 编译 FFMPEG
- 数据结构(块状链表):COGS 1689. [HNOI2010]Bounce 弹飞绵羊
- UVA 1622 Robot
- 【Python】考虑用生成器改写直接返回列表的函数
- WCF(三)分布式事务
- lintcode.66 二叉树前序遍历
- PHP与XML
- API创建员工地址
- SparkStreaming+Kafa+HBase
- js中关于数组处理的一些小技巧
- 重新安装liteide后无法关联.go文件的解决办法(及更改liteide配色方案)