How does phoneGap (Cordova) work internally, iOS specific

前端 未结 2 675
粉色の甜心
粉色の甜心 2021-02-07 21:46

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.

相关标签:
2条回答
  • 2021-02-07 22:13

    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.

    0 讨论(0)
  • 2021-02-07 22:25

    I am trying to figure this out in more detail, too. Basically there are 2 Methods on the iOS side that can help ...

    • - webView:shouldStartLoadWithRequest:navigationType: and
    • - stringByEvaluatingJavaScriptFromString:script

    From the sources it seems cordova sends a "READY" message using webView:shouldStartLoadWithRequest:... and then picks up results with the second message, but I am not sure.

    Cordova Sources iOSExec

    There is much to learn there.

    0 讨论(0)
提交回复
热议问题