Giter Club home page Giter Club logo

easyjswebview's Introduction

EasyJSWebView - much simpler JS X Obj-C interaction

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

dukeland avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

easyjswebview's Issues

how to send array value for js methods

  1. interfacename : app
  2. js method for html page (test.html)
    var keys = ['title','header'];
    alert(app.testwitharrayret(keys));
  3. how to excute the params for array
  4. (NSArray_)testwitharrayret:(EasyJSDataFunction_)params{
    NSLog(@"test with param: %@ ", params);
    NSArray *obj_arr = [NSArray arrayWithObjects:@"share",@"social", nil];
    params.removeAfterExecute = NO;
    return [params executeWithParams:obj_arr];
    }

hi dukeland. kindly canplease help me this issue

EasyJSWebView not working with PhoneGap

Hello. My app can't call 'addJavascriptInterfaces' method with PhoneGap to iOS. Can't find 'MyJSTest' js variable. I checked all the #import declarations. Any idea? Thank you in advance.

Break on reload

In using this library I have noticed that the functionality breaks when reloading a page or navigating to a new page.

_webView.delegate = self;

javaScriptInterface* interface = [javaScriptInterface new];

[self.webView addJavascriptInterfaces:interface WithName:@"interface"];

interface.webView = self;

[interface release];

[[NSURLCache sharedURLCache] removeAllCachedResponses];

 [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"mywebpage.com"]]];

This is the code I use for an initial load of the page. If I call another loadrequest on the same webview the communication between javascript and obj-c stops working. I am working with a web page that is designed to make a lot of calls to these interface functions, and without them the webpage is broken.

This also doesn't work if I try to add a new interface before reloading.

Is there any work around or fix to this? At the moment I am creating a new webview everytime I need to reload and destroying the old one, but would like to use one webview for everything.

Add License to Project

My team and I are interested in using this library but unfortunately cannot until you release the project with an open source license (preferably MIT, BSD, or Apache). Thanks!

Callbacks are ignored sometimes

It seems that sometimes, callbacks are ignored. It appears notably when we call multiple times the same interface method in a small gap of time.

This is caused by iframe loading in easyjs-inject.js :

iframe.setAttribute("src", "easy-js:" + obj + ":" + encodeURIComponent(functionName) + argStr);
document.documentElement.appendChild(iframe);
iframe.parentNode.removeChild(iframe);
iframe = null;

This triggers a call to (void)webViewDidStartLoad:(UIWebView *) in EasyJSWebViewProxyDelegate that reinjects again the EasyJS callback handling, resetting the EasyJS.__callbacks variable to an empty array, forgotting any other callbacks in progress.

Solved

Solved by injecting the EasyJS javascript stuff ONLY one time, by checking a boolean variable set to true when the javascript stuff is injected.

In the same method :

if (!_injectJSDone) {
        NSString* js = INJECT_JS;
        //inject the basic functions first
        [webView stringByEvaluatingJavaScriptFromString:js];
        //inject the function interface
        [webView stringByEvaluatingJavaScriptFromString:injection];
        
        _injectJSDone = YES;
    }

Name change

I am unable to change my MyJSTest with SomeMethod
```
/*
A sample URL structure:
easy-js:SomeMethod:test
easy-js:SomeMethod:testWithParam%3A:haha
*/



My HTML Page change 

function onReady()
{
var result= SomeMethod.getUserInfo();
alert(result);
}


And this is the way i calling it

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

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.