本文包括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(, , [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

最新文章

  1. Python Day18
  2. hdu 2896 字典树解法
  3. Android--多线程之Handler(转)
  4. Leetcode 235 Lowest Common Ancestor of a Binary Search Tree 二叉树
  5. UITabBar 设置字体的颜色(选中状态/正常状态)setTitleTextAttributes
  6. 【T-SQL系列】FOR XML PATH 语句的应用
  7. 从github上面拷贝源码
  8. JS 基于面向对象的 轮播图2
  9. 理解JavaScript的定时器与回调机制
  10. 64位 CentOS NDK 编译 FFMPEG
  11. 数据结构(块状链表):COGS 1689. [HNOI2010]Bounce 弹飞绵羊
  12. UVA 1622 Robot
  13. 【Python】考虑用生成器改写直接返回列表的函数
  14. WCF(三)分布式事务
  15. lintcode.66 二叉树前序遍历
  16. PHP与XML
  17. API创建员工地址
  18. SparkStreaming+Kafa+HBase
  19. js中关于数组处理的一些小技巧
  20. 重新安装liteide后无法关联.go文件的解决办法(及更改liteide配色方案)

热门文章

  1. Angular进阶教程三
  2. 计算球面两点间距离实现Vincenty+Haversine
  3. iphone崩溃日志分析工具symbolicatecrash用法
  4. 【Oracle】数据库中sql%notfound的用法
  5. MySQL5.7.20编译安装
  6. 计算机作业(Excel工资表) 物联网 王罗红
  7. 第六次作业——Excel制作工资表
  8. SharePoint2007深入浅出——使用jQuery UI
  9. AngularJs学习笔记--Using $location
  10. 环境变量、block、修饰符:block对环境变量的引用和修改需要通过修饰符来限定