Previous section address http://blog.csdn.net/lwjok2007/article/details/47058101
And then the last verse we talked about iOS call JS
Down we use JS call iOS
JS calls iOS in two different cases
One, JS inside the direct call method
Second, JS inside through the object call method
First, we look at the first, calling the method directly.
which uses the block of iOS.
On the Code
-(void) Webviewdidfinishload: (UIWebView *) webview{ //page load Complete Call this method //ios call JS //First create Jscontext Object (here through the current WebView key gets to Jscontext) jscontext *context=[webview valueforkeypath:@ " DocumentView.webView.mainFrame.javaScriptContext "]; JS call iOS //First case //where Test1 is the method name of JS, assigned to is a block inside is iOS code //This method will eventually print out all the received parameters, JS parameter is not fixed we'll know when we test. context[@ "test1"] = ^ () { Nsarray *args = [Jscontext currentarguments]; for (id obj in args) { NSLog (@ "%@", obj); } }; Here we do not write backstage (but we already know that iOS can call JS, we simulate) //First prepare the JS code, to invoke the JS function test1 and then execute //a parameter nsstring *[email Protected] "test1 (' parameter 1 ')"; [Context Evaluatescript:jsfunctstr]; Two parameters nsstring *[email protected] "test1 (' parameter a ', ' parameter B ')"; [Context evaluatescript:jsfunctstr1]; }
If the block assigned to Test1 in the above code is executed then the result is right, JS calls the iOS
We look at the results.
We see the parameters are printed out, and a parameter and two parameters can be, also conform to the JS parameter number of unlimited
Down we see the second case is that the method is invoked by an object in JS.
It's a little bit more complicated here, we need to use
Jsexport
All things added Jsexport protocol protocol, the prescribed methods, variables, etc. will be open to JS, we can call through JS to
It's a bit around here. We look directly at the code
First create a class to inherit NSObject and specify a protocol
#import <Foundation/Foundation.h> #import <javascriptcore/javascriptcore.h>// First create a protocol that implements the Jsexport protocol @protocol testjsobjectprotocol <jsexport>//here we test several parameters-(void) testnoparameter;-( void) Testoneparameter: (NSString *) message;-(void) Testtowparameter: (NSString *) message1 secondparameter: (NSString * ) Message2; @end//Let us create a class that implements the above protocol @interface testjsobject:nsobject<testjsobjectprotocol> @end
Implementation of the class
#import "TestJSObject.h" @implementation testjsobject//The method is just a log and so will look at the log and parameters can be on the instructions JS called here the native method of iOS-(void) testnoparameter{ NSLog (@ "This is iOS testnoparameter"); -(void) Testoneparameter: (NSString *) message{ NSLog (@ "This is iOS testoneparameter=%@", message); -(void) Testtowparameter: (NSString *) message1 secondparameter: (NSString *) message2{ NSLog (@ "This is iOS testtowparameter=%@ second=%@ ", Message1,message2);} @end
Let's call the test after Weibview loading is complete.
The code is as follows:
-(void) Webviewdidfinishload: (UIWebView *) webview{ //page load Complete Call this method //Create Jscontext First Object (here through the current WebView key gets to Jscontext) jscontext *context=[webview valueforkeypath:@ " DocumentView.webView.mainFrame.javaScriptContext "]; In the second case, JS is called through the object, we assume that JS has an object Testobject in the calling method //First create our new class object, assign him to the object JS testjsobject *testjo=[ Testjsobject new]; context[@ "Testobject"]=TESTJO; We also use the same way to simulate the JS call method nsstring *[email protected] "testobject. Testnoparameter () "; [Context Evaluatescript:jsstr1]; NSString *[email protected] "testobject. Testoneparameter (' parameter 1 ') "; [Context EVALUATESCRIPT:JSSTR2]; NSString *[email protected] "testobject. Testtowparametersecondparameter (' parameter a ', ' parameter B ') "; [Context EVALUATESCRIPT:JSSTR3]; }
Results such as
Okay, we saw the results. Three methods are called
OK, two ways are done.
Source code We upload to the group space [testjsoc.zip] need to download
Apple Development Group: 414319235 Welcome to join the Welcome discussion question
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
IOS JS OC calls each other (JavaScriptCore) (ii)