以前一個ios項目中,須要經過UIWebview來打開一個靜態頁面,並在靜態頁面中javascript
調用相關object-c代碼。html
1、之前使用js調用object-c的方法java
關於如何使用javascript調用object-c中的函數和方法,我搜索了很久ios
網上全部的方法,基本都指明瞭一個方向,那就是在UIWebview中載入的js代碼中web
經過改變document.locations=「」,而後回調UIWebview的objective-c
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationTypeide
函數,在上面這個函數中,經過截取NSURLRequest解析js中傳遞過來的參數,再根據參數函數
來選擇早已定義好的方法。this
有沒有別的方法呢? 我找了幾個月,沒找到!可是有一個起色。lua
我偶然知道了 javascriptCore.framework 這個庫
因而事情有了起色。
2、在js中直接調用oc的方法
廢話很少說,如今看看如何在UIWebView的javascript中調用oc的方法
首先在創建一個UIWebView,代碼以下:
//
// webview.m
// login
//
// Created by wangdan on 15-3-19.
// Copyright (c) 2015年 wangdan. All rights reserved.
//
#import "webview.h"
#import <javascriptcore javascriptcore.h="">
@implementation webview
-(id)initWithFrame:(CGRect)frame
{
self=[super initWithFrame:frame];
if( self ){
self.webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 310, self.bounds.size.width, 300)];
self.webview.backgroundColor=[UIColor lightGrayColor];
NSString *htmlPath=[[NSBundle mainBundle] resourcePath];
htmlPath=[htmlPath stringByAppendingPathComponent:@"html/index.html"];
NSURL *localURL=[[NSURL alloc]initFileURLWithPath:htmlPath];
[self.webview loadRequest:[NSURLRequest requestWithURL:localURL]];
[self addSubview:self.webview];
JSContext *context = [self.webview valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
context[@"log"] = ^() {
NSLog(@"+++++++Begin Log+++++++");
NSArray *args = [JSContext currentArguments];
for (JSValue *jsVal in args) {
NSLog(@"%@", jsVal);
}
JSValue *this = [JSContext currentThis];
NSLog(@"this: %@",this);
NSLog(@"-------End Log-------");
};
// [context evaluateScript:@"log('ider', [7, 21], { hello:'world', js:100 });"];
}
return self;
}
@end</javascriptcore>
(1)在上述代碼中,使用javascriptCore.framework,首先使用UIWebview加載一個靜態網頁,並
使用
JSContext *context = [self.webview valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
獲取該UIWebview的javascript執行環境。
(2)在該javascript執行環境中,定義一個js函數,注意關鍵點來了
這個函數的執行體徹底是 objective-c代碼寫的,也就是下面:
context[@"jakilllog"] = ^() {
NSLog(@"Begin Log");
NSArray *args = [JSContext currentArguments];
for (JSValue *jsVal in args) {
NSLog(@"%@", jsVal);
}
JSValue *this = [JSContext currentThis];
NSLog(@"-------End Log-------");
};
(3)試想一下,在定義的webview中,若是使用js執行log這個函數,那麼會不會調用上面oc中block段代碼呢,答案是確定的!
下面看看UIWebView 中所加載的 html及其js代碼是如何寫的
(4)index.html代碼
<!--// Created by wangdan on 15-3-19.-->
<!--// Copyright (c) 2014年 wangdan. All rights reserved.-->
<script type="text/javascript" src="index.js"></script>點擊button
上面html定義了一個button,而後引用index.js,點擊button的響應函數爲buttonClick()
該函數在index.js中定義,以下
function buttonClick()
{
jakilllog("hello world");
}
意思是點擊這個button,就調用jakilllog()函數,jakilllog()函數顯然是咱們在oc中實現的一個block段,
就是上述綠色部分的block段。
點擊button會執行麼?答案是確定的。
下面上圖
下圖是執行的結果
點擊html中的button,可以執行oc中的代碼
說明直接從js調用oc的意圖達到。