GithubHelp home page GithubHelp logo

edison0951 / easyjswebview Goto Github PK

View Code? Open in Web Editor NEW

This project forked from andiradulescu/easyjswebview

0.0 2.0 0.0 195 KB

Enable adding Javascript Interface to UIWebView like Android WebView

Objective-C 87.36% JavaScript 12.64%

easyjswebview's Introduction

EasyJSWebView - much simpler JS X Obj-C interaction

This repository was forked from https://github.com/dukeland/EasyJSWebView

Since the original version breaks on redirects in the webview, I changed it to work after every page load (webViewDidFinishLoad).

Because of this modification, if you try to call the bridge (e.g. MyJSTest.test() from the sample), without waiting for the page to load, it will NOT work.

Try this instead:

setTimeout(function() { MyJSTest.test() }, 1);

###Introduction You are using UIWebView in your iOS app and you want to do some communications between the Javascript inside the WebView and Objective-C. How would you do it?

To run Javascript in Objective-C, you can use the โ€“ stringByEvaluatingJavaScriptFromString: method. To run Objective-C method, well it is a little bit tricky, you need to implement the UIWebViewDelegate and the shouldStartLoadWithRequest method.

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).

EasyJSWebView is a library that allows you to do the same in Objective-C. Download it and try. I promise. It is much simpler to do the job!!!

You may find the sample project here.

###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";
});

Simple, huh!?

Try it now!!!

###Some simple facts

  • NSInvocation does not live peacefully with ARC. This library is thus a non-ARC library.
  • It supports only NSString* for message passing now.
  • We are Dukeland from Hong Kong! A group of IT-holic guys

easyjswebview's People

Contributors

andiradulescu avatar dukeland avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.