phoneGap(Cordova)如何在内部工作,iOS特定 [英] How does phoneGap (Cordova) work internally, iOS specific

查看:152
本文介绍了phoneGap(Cordova)如何在内部工作,iOS特定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始为多个平台开发html应用程序。我最近听说过Cordova 2.0(PhoneGap),因为我很想知道这座桥是如何工作的。
在走了很多代码后,我看到Exec.js是从JS调用的代码 - > Native发生了

  execXhr = execXhr || new XMLHttpRequest(); 
//将此更改为GET将使XHR在4.2上达到URIProtocol。
//因为某种原因它仍然不工作,但...
execXhr.open('HEAD',file:///!gap_exec,true);
execXhr.setRequestHeader('vc',cordova.iOSVCAddr);
if(shouldBundleCommandJson()){
execXhr.setRequestHeader('cmds',nativecomm());
}
execXhr.send(null);
} else {
execIframe = execIframe || createExecIframe();
execIframe.src =gap:// ready;

但是想了解它是如何工作的,这里的概念是什么,file:/// !gap_exec或gap://准备好了吗?和



>解决方案

诀窍很容易:



有一个webview。这将显示您的应用程序。



如果浏览器浏览至:

  file:///!gap_exec 

 差距:// 

取消导航。这些字符串后面的所有内容都被重新用作标识符,以获取具体的plugin / plugin-method和参数:



伪url示例:

  gap:// echoplugin / echothistext?Hello World 

这将导致phonegap寻找 echoplugin 并调用 echothistext 方法发送



更新 p>

从本地到javascript的方法是(或可能)在webview中加载 javascript: url。



具体的实现有点复杂,因为javascript必须发送一个callback-id到本地代码。可能有多个本机调用同时运行。但事实上这不是魔术。只是一个数字,以获得正确的JSON到正确的javascript回调。



在平台和javascript之间有不同的通信方式。对于Android,有三个或四个不同的网桥。


I have started developing html applications for mutliple platforms. I recently heard about Cordova 2.0(PhoneGap) and ever since I have been curious to know how the bridge works. After lot of code walking, i saw that the Exec.js is the code where call from JS -> Native happens

execXhr = execXhr || new XMLHttpRequest();
        // Changeing this to a GET will make the XHR reach the URIProtocol on 4.2.
        // For some reason it still doesn't work though...
        execXhr.open('HEAD', "file:///!gap_exec", true);
        execXhr.setRequestHeader('vc', cordova.iOSVCAddr);
        if (shouldBundleCommandJson()) {
            execXhr.setRequestHeader('cmds', nativecomm());
        }
        execXhr.send(null);
    } else {
        execIframe = execIframe || createExecIframe();
        execIframe.src = "gap://ready";

But want to understand how that works, what is the concept here, what does file:///!gap_exec or gap://ready do? and how does the call propgate to the lower layers (native code layers)

thanks a bunch in advance.

解决方案

The trick is easy:

There is a webview. This displays your app. The webview will handle all navigation events.

If the browser navigates to:

file:///!gap_exec 

or

gap://

the webview will cancel the navigation. Everything behind these strings is re-used as an identifier, to get the concrete plugin/plugin-method and parameter:

pseudo-url example:

gap://echoplugin/echothistext?Hello World

This will cause phonegap to look for an echoplugin and call the echothistext method to send the text "Hello World" to the (native) plugin.

update

The way back from native to javascript is (or may be) loading a javascript: url into the webview.

The concrete implementation is a little bit more complex, because the javascript has to send a callback-id to native code. There could be more than one native call are running at the same time. But in fact this is no magic at all. Just a number to get the correct JSON to the right javascript-callback.

There are different ways to communicate between the platform and javascript. For Android there are three or four different bridges.

这篇关于phoneGap(Cordova)如何在内部工作,iOS特定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆