CocoaPods trunk is moving to be read-only. Read more on the blog, there are 19 months to go.

WKWebViewJSBridge 1.2.0

WKWebViewJSBridge 1.2.0

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Aug 2017

Maintained by zhs_zy.



  • By
  • zhszy

WKWebViewJSDemo

Do you know how to do this in Android? You simply need to create a class and pass an instance to the WebView through addJavascriptInterface(Object object, String name).

###Some code to demonstrate So basically what you need to do is create a class like this.

@interface MyJSInterface : NSObject

- (void) test;
- (void) testWithParam: (NSString*) param;
- (void) testWithTwoParam: (NSString*) param AndParam2: (NSString*) param2;

- (NSString*) testWithRet;

@end

Then add the interface to your UIWebView.

MyJSInterface* interface = [MyJSInterface new];
[self.myWebView addJavascriptInterfaces:interface WithName:@"MyJSTest"];
[interface release];

In Javascript, you can call the Objective-C methods by this simple code.

MyJSTest.test();
MyJSTest.testWithParam("ha:ha");
MyJSTest.testWithTwoParamAndParam2("haha1", "haha2");

var str = MyJSTest.testWithRet();

Just that simple!!! EasyJSWebView will help you do the injection. And you do not even need to use async-style writing to get the return value!!!

But of course, sometimes we may need to use the async-style code. It is also supported. You can even get the return value from the callback function.

- (void) testWithFuncParam: (EasyJSDataFunction*) param{
  NSLog(@"test with func");
	
	NSString* ret = [param executeWithParam:@"blabla:\"bla"];
	
	NSLog(@"Return value from callback: %@", ret);
}

And in Javascript,

MyJSTest.testWithFuncParam(function (data){
	alert(data); //data would be blabla:"bla
	return "some data";
});

注:此项目参考EasyJSWebView进行封装