JS interacting with iOS Android

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.