In the previous blog has been chatting with you about JS and Android, iOS native control communication between the detailed code implementation, today we talk about JS call Android, iOS communication similarities and differences, in order to help us in the hybrid development, improve the quality of code, Implementation of the two in the web-side code unification.
First we look at the iOS call JS method implementation:
// no parameter calls function SwiftCallJs1 () {} // called function SwiftCallJs2 (name, message) {}
Then we look at the Android call JS method implementation:
// no parameter calls function AndroidCallJs1 () {} // Call function AndroidCallJs2 (data) {}
From the above code, do you find that when Android and iOS call JS, the JS processing function is the same when the non-parameter call. The difference is that iOS is one-to-one delivery, Android is a string transfer, of course, we can use the JSON format on the Android and iOS side of the unified parameters, JS support JSON parsing, so that two platforms on the call of the unified method.
Read the above iOS and Android call JS, below we look at JS call Android and iOS native communication similarities and differences.
First, we look at the JS call Android method implementation:
// no parameter calls function Callnull () { Android. Callnull (); } // called with a reference function Callmessage () { Android. Callmessage ("msg");} // call--json with reference function Calljson () { var"[{\" name\ ": \" full art net \ ", \" url\ ": \" Www.manyiaby.com\ "}]"; Android. Calljson (JSON);}
Next we look at the JS call iOS method implementation:
//One: no prefix calls//no parameter callsfunction Test1 () {Test1 ();}//called with a referencefunction Test2 () {Test2 ("Full Art net","www.manyiaby.com");}//Two: There are prefix calls//no parameter call--with return operation result processingfunction Callsystem () {Ios.callsystem ();}//Action Result handler functionfunction Jscallnulliosresult () {}//called with a referencefunction callwithmsg () {ios.callwithmsg ("Full Art net","www.manyiaby.com");}//call--json with referencefunction Callwithjson () {varJSON ="{\ "name\": \ "full art net \", \ "url\": \ "www.manyiaby.com\"}"; Ios.callwithjson (JSON);}//called with a call--with return result processingfunction Callwithjsonresult () {varJSON ="{\ "name\": \ "full art net \", \ "url\": \ "www.manyiaby.com\"}"; Ios.callwithjsonresult (JSON);} function Jscalljsoniosresult (data) {alert ("Name:"+data["name"]); }
See here you must have found a different point, 1, Android does not have a method call without a prefix, 2, Android no method call after the result of the operation returned processing logic. For the 1th, what I now realize is that the native does not pass the specific implementation logic, but this point does not affect our development, because iOS supports prefix method invocation. For the 2nd, my understanding is that for the return handler, we can add to the development to improve the iOS user experience, and the processing function does not affect the invocation of the method.
All right, here we have a chat with you, of course, if the said and your understanding is biased, but also hope pointing twos.
JS calls Android, iOS native control