1. Through UIWebView and JS interaction
Native and server interaction through Web pages so easy. Of course, to achieve the delegate of WebView. Self.webView.delegate = self;
1.1 iOS Client Calls JS method.
Directly using the method of the system Stringbyevaluatingjavascriptfromstring call JS method can be.
[_webview stringbyevaluatingjavascriptfromstring:@ "Smallfont ()"];
* Note: Smallfont () is the method of JS.
1.2 js to call iOS method.
Compared to Android, iOS is more cumbersome to call, because Android has a way to be called, and iOS is more reserved, it is not directly to be called
1. First we have to drink the server side to define a good method and logo. For example, the server definition of the js2ios://{"functionname": "BuyNow", "args": "88888888"} means that my parameters in the band Js2ios, I want to interact with you. You give me a way to achieve buynow ( functionname) I'm going to use the parameter (args) 88888888 to call.
2. Because iOS can only be in the URL redirect when the implementation is JS call, so the premise is that I click on the page to interact with the button and so on, to be able to jump into my
-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype: ( Uiwebviewnavigationtype) Navigationtype method, if you do not enter, you can tell the server's brother said: Hey, handsome, your code may be a bit of a problem, if you make sure you realize the WebView delegate, which is Self.webView.delegate = self;
3. We are now in-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype: ( Uiwebviewnavigationtype) navigationtype to intercept the URL.
-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype: ( Uiwebviewnavigationtype) Navigationtype {
if ([Request. Url.scheme isequaltostring:@ "Js2ios"]) {
Decode
NSString *DECODEURLSTR1 = [Request. Url.absolutestring stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding];
NSString *decodeurlstr;
SEL Selctor = nsselectorfromstring (the method name to be intercepted);
IMP imp = [self methodforselector:selctor];
void (*func) (Id,sel) = (void *) imp;
Func (Self,selctor);
[Self performselector:selctor withobject:nil];
The method that calls the annotation directly here has a warning that you can refer to: My article <ios performselector may cause a leak because it selector is unknown>
Of course, if you do not want to load a specific URL, or load a specific URL to do special processing, you can intercept here
}
return YES;
}
2. Run-time Interaction
Study hard ...