Loading remote html in PhoneGap or Cleaver (Cordova) on iOS

只谈情不闲聊 提交于 2019-12-03 04:36:50

问题


I am using an Cordova 2.4 component Cleaver and an embedded view in my native iOS 6 application. So far I have managed to create the project structure, link the Cordova libraries and set up the Hello World app that does work providing "Device Ready" feedback.

This is all great but it loads all html from the www repository distributed inside the app itself (including all js libraries).

What I really want yo do is this:

1 - popup the cleaver component (which is nothing else than an embedded uiwebview) in my app. Easy - done.

2 - load some html content from a URL pointing to a servlet on my remote server. I have several servlets and need to be able to load each one of them separately of course.

3- have the content generated in step 2 interact with my native app via the cordova javascript libs cordova-2.4.0.js - (how do I load these if they are local to device but html was loaded from remote location).

How can I set this up ?

P.S.

I am more of Obj-C than Javascript developer :)


回答1:


Here is the answer. What a joy...

excellent article on dynamic page loading in PhoneGap and Cordova

Precisely what I needed. The second part of the project was to enable native code to force the loading of external web services - I accomplished this by called stringByEvaluatingJavaScriptFromString on the Cleaver web view .

[webview stringByEvaluatingJavaScriptFromString:@"app.loadExternal('www.usatoday.com')]; is the code that works like a charm:)

Viola - I have a Cleaver view capable of loading external html content with complete two-way communication between the javascript app and the native container.




回答2:


Inside your index.html file do something like this (for the point 2)

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
<meta charset="utf-8">
<script type="text/javascript" charset="utf-8" src="cordova-2.1.0.js"></script>
<script type="text/javascript">
     function onBodyLoad(){     
        document.addEventListener("deviceready", onDeviceReady, false);
     }
     function onDeviceReady(){
         window.location.href = <your_remote_url>
     }
}
</script>

For the point 3, your remote content should import cordova.js and the interaction (native / web) will work as if it was local content.



来源:https://stackoverflow.com/questions/14818138/loading-remote-html-in-phonegap-or-cleaver-cordova-on-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!