iOS JS interaction is divided into two blocks: 1.oc call JS
This piece is relatively simple to implement.
My project is loaded with local html,js,css, and it is important to note that when you drag the files into the project, you choose to copy to the project, (the folder you dragged is blue, relative to the path), or the path to the CSS,JS is problematic.
Load Local HTML:
OC Call JS: a code to fix
2.js call OC JS call OC is divided into two kinds: 1.js is the direct call method
Here's a new API JavaScriptCore introduced by IOS7, first we introduce this class and initialize a Jscontext object
In the Webviewdidfinishload method.
This is obtained through the current WebView key to Jscontext
Connect our context object with the JS method to establish a bridging connection,
2.js end-of-object invocation method
When it comes to object tuning, we must first have an object, so we first need to create a JS interaction class, and declare and implement the corresponding interactive method, here we need to note that we need to import the JavaScriptCore framework in this custom interaction class, Declare a protocol that complies with the Jsexport protocol (a bit around, look at the picture), declare our interaction method in this protocol, and finally let our interaction class conform to our custom protocol
Implement the corresponding JS interaction method in the. m of the interaction class, and implement the action we want
Notice that on the diagram
Jsexportas(Invoke,-(void) Invoke: (nsstring *) method args: (nsstring *) args);
This method does not, the function of this method is when the JS side of the method has two or more than two parameters, we need to let-(void) Invoke: (nsstring *) method args: (nsstring *) args; This method replaces the corresponding JS method, because the OC side of the method name must be consistent with the JS side, otherwise it will not be called
Finally, we initialize our interaction object in our Webviewdidfinishload method and let the context connect with it, OK, done
Hybridapi calls this method--
3. Objective-c calls the JavaScript method code as follows:
[self. WebView evaluatejavascript: [nsstring stringwithformat:@ "__yutaapponprepare ()" ]
Completionhandler: ^ (ID response, nserror *error)
{
}];
The use of IOS JS interaction system Jscontext Implementation JS Call OC method