IOS and android interaction

This is the first time to do mixed development with native apps, and some of the active pages need to be done with h5 on my side.

Then there must be a problem. It is to interact with the native app.

Divided into ios and android. The methods are different, and the logic is the same.

The general idea is that I and the app side agree on a method, similar to? Connector code?

For example, we agreed on a method: 'blue sky and white clouds', then when I call this method from js, the app side recognizes me through 'blue sky and white clouds' and responds.

Then the question comes, many students (probably only myself) will say, how to call the functions defined by my native js, how to call his app, and how to know that I have adjusted the method.

****After communicating with the app side, the bottom layer in the original app, or in the framework, will define a method to accept our js function**** (don't care here, we just need to care what method we call)

code above

           //定义传的参数
          let sendData = {
                url: "articleDetail",
                id: v.id
            };
             //判断运行环境(搜一下)
            if (this.isiOS) {
       //解释一下,这里定义的window.xxxx什么方法,我们的window对象下是没有这个方法的,只有当我们的程序,运行到了适当的环境中,app会给我们的window对象添加这个方法,让我们可以调用,所以用了try防止报错
   //下面的callIos和callAndroid就是我和app大兄弟约定的方法
                try {
                    window.webkit.messageHandlers.callIos.postMessage(
                        JSON.stringify(sendData)
                    );
                } catch (e) {}
            } else {
                try {
                    window.jsInterface.invokeMethod("callAndroid", [
                        JSON.stringify(sendData)
                    ]);
                } catch (e) {}
            }

 

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324078375&siteId=291194637