①ios-JS
Principle: Directly invoke JS code by WebView method
[WebView stringbyevaluatingjavascriptfromstring:@ "A"];
Here's A is the webview in the HTML corresponding to the JS code
eg.
1) Here a public method within the JS file contained in the HTML
2) This is a. m (iOS) file within a WebView call 1) in the notation of the JS method
3) in the corresponding method of JS, you will get the parameters from iOS
IOS ②js
Principle: iOS WebView protocol function callback can get to its internal HTML element changes and corresponding values, JS side only need to change the internal elements can
Step one: The processing of JS terminal
Step two: JS side add child elements after the iOS side callback
Webview-uiwebviewdelegate
-(BOOL) WebView: (UIWebView *) Thewebview shouldstartloadwithrequest: (nsmutableurlrequest *) Request Navigationtype: ( Uiwebviewnavigationtype) navigationtype{
//Get the request string after the JS end element changed (get BC? D=d)
NSString *reqstring = [[Request URL] absolutestring] stringbyreplacingpercentescapesusingencoding: Nsutf8stringencoding];
B can be used as a judge of flag
C is the method name of the calling iOS
// ? As a delimiter
D is the parameter name
= equals number, no meaning.
d for specific parameters (please transfer in JSON format, because the parameters of iOS sel are limited)
The SEL object is obtained after the method name and parameters are obtained
Sel sel = nsselectorfromstring ([methodName stringbyappendingstring:@ ":"]);
You can also get the parameter string to dictionary
NSData *jsonData = [C dataUsingEncoding:NSUTF8StringEncoding]; NSError *err; NSDictionary *argsInfo = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err]; //method has, parameter also has, call, parameter is given in the form of dictionary
if ([self Respondstoselector:sel]) {
Call method C, the parameter is a dictionary (the parameters of method C in iOS should also be dictionary)
[Self Performselector:sel withobject:argsinfo];
}
}
iOS and JS interaction