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.
When using the block of iOS
On the Code
-(void) Webviewdidfinishload: (UIWebView *) webview{ //page loading 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 //in which Test1 is the method name of JS, assigned to is a block inside is iOS code //This method will finally print out all the received parameters. JS parameters are not fixed we know 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 is capable of invoking JS.) We simulate it) //First prepare the JS code, to invoke the JS function test1 and then run //a parameter nsstring *[email protected] "test1 (' 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 running then the result is right, JS calls the iOS
We look at the results.
We saw that the parameters were printed out. and a number of parameters and two can be, but also in line with the number of JS parameters Unlimited
Let's see. Another scenario is that JS is called by an object.
It's a little bit more complicated here, we need to use
Jsexport
All things added to the Jsexport protocol, the prescribed methods, variables, etc. will be open to JS, we can call through JS to
It is simpler to assume that JS is a parameter or not, and that our method name and JS method name are consistent.
For example, the JS method is
Testobject. Testoneparameter (' number 1 ')
Then the proxy method that we add to OC is
-(void) Testoneparameter: (NSString *) message;
Assuming that JS is a number of arguments, the name of all the variables in our proxy method will be the same as the JS method name.
For example, the JS method is
Testobject. Testtowparametersecondparameter (' parameter a ', ' parameter B ')
He has two parameters. Then our proxy method is to split the JS method name Testtowparametersecondparameter randomly into two segments as the proxy method name (here we split into Testtowparameter and Secondparameter) So our proxy method is to
-(void) Testtowparameter: (NSString *) message1 secondparameter: (NSString *) Message2;
We see the proxy method has two variable aliases
Testtowparameter and Secondparameter
He's two, and it's just our JS method name.
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//method is just a log and so will look at the log and the number of parameters can be explained by JS called 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
Below we call test after Weibview loading is complete
The code is as follows:
-(void) Webviewdidfinishload: (UIWebView *) webview{ //page loading 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 another case, JS is called through the object. If JS has an object Testobject in the Call method //First create the object of our new class. Assign him to JS object testjsobject *testjo=[testjsobject new]; context[@ "Testobject"]=TESTJO; Same We also use just the way to simulate the JS call method nsstring *[email protected] "testobject. Testnoparameter () "; [Context Evaluatescript:jsstr1]; NSString *[email protected] "testobject. Testoneparameter (' number 1 '); [Context EVALUATESCRIPT:JSSTR2]; NSString *[email protected] "testobject. Testtowparametersecondparameter (' number a ', ' parameter B '); [Context EVALUATESCRIPT:JSSTR3]; }
Results for example with
Okay, we saw the results. Three methods are called
OK two ways are finished
Source we upload to the group space [testjsoc.zip] need to download
Apple Development Group: 414319235 Welcome to add welcome discussion questions
IOS JS OC calls each other (JavaScriptCore) (ii)