One. Android interaction
1.js Call WebView
In the Android API level 17 and above, there will be JS can not call Android code, this is version compatibility problem, need to add an annotation on the calling method: @JavascriptInterface, This annotation needs to import a package: import Android.webkit.JavascriptInterface;
Public voidonCreate (Bundle savedinstancestate) {//method to set the call to JS This. Appview.addjavascriptinterface (NewJavascripdtobject (), "Android");//This android is injected into the object}/*** Method of object for JS call **/ Public classJavascripdtobject {@JavascriptInterface//Add this note Public voidRunandroidmethod (String str) {toast.maketext (mainactivity. This, "Called by JS" +str, toast.length_short). Show ();
}}
JS Code
<script> Window.android.runAndroidMethod (str); </script>
2. Android Call JS this simple
This.appView.getSettings (). setjavascriptenabled (true); This.appView.loadUrl ("javascript: View.test () ");
Two. js Call iOS
- (void) Addshowtypeview: (NSString *) nibname{uiapplication*app =[UIApplication sharedapplication]; Appdelegate*delegeat = App.Delegate; Self.creativeview= [[NSBundle mainbundle] Loadnibnamed:nibname owner:self options:nil][0]; __weak __typeof__ (self) self_weak=Self ; //returnSelf.creativeView.backBtnBlock = ^ (UIView *Sideview) {[Self_weak backBtnClickMainView:self_weak.webView View:sideview]; }; Self.creativeView.frame= CGRectMake (Kscreenwidth,0, kscreenwidth/2, Kscreenheight); [Delegeat.window Addsubview:_creativeview]; [Self TranslationViewAnimationMainView:self.webView RightView:self.creativeView];}#pragmaMark-uiwebview Agent/** * Web page JS dynamic call OC Method!!! *///This proxy method is related to loading the page-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (Nsurlrequest *Request Navigationtype: (uiwebviewnavigationtype) navigationtype{nsstring*urlstring =[[Request URL] absolutestring]; DLog (@"url =%@", urlstring); Nsarray*urlcomps = [URLString componentsseparatedbystring:@"://"]; if([Urlcomps Count] && [[Urlcomps Objectatindex:0] Isequaltostring:@"OBJC"]) { if(Urlcomps.count >1) { if([urlcomps[1] Hasprefix:@"Showtypeview"]) {Nsarray*arr = [urlcomps[1] Componentsseparatedbystring:@"&"]; if(Arr.count >0) {sel sel= Nsselectorfromstring (arr[0]); if([self Respondstoselector:sel]) {# pragma clang diagnostic push# pragma clang diagnostic ignored"-warc-performselector-leaks"[Self Performselector:sel withobject: ((Arr.count>1)? arr[1]:nil)];# pragma clang diagnostic Pop}}}Else if([urlcomps[1] Hasprefix:@"Showshareview"]) {Nsarray*arr = [urlcomps[1] Componentsseparatedbystring:@"&"]; if(Arr.count >0) {sel sel= Nsselectorfromstring (arr[0]); if([self Respondstoselector:sel]) {# pragma clang diagnostic push# pragma clang diagnostic ignored"-warc-performselector-leaks"[Self Performselector:sel withobject: ((Arr.count>1)? arr[1]:nil)];# pragma clang diagnostic Pop}}}} returnYES;}
JS Code
Document.location= "Objc://showtypeview:&1";
JS interacting with iOS Android