我必须以下情况:
我有一个已经存在的远程网页,我想开发一个使用这个页面的应用程序。
到目前为止,一切都很好。当我启动应用程序时,会加载本地 index.html
并将 (window.open target: _self
) 重定向到外部网站。该网站在 phonegap webview 中打开。在外部网站上,我添加了 cordova.js
以访问 native phonegap API。但它不能正常工作。 deviceReady 事件被正确触发,但我无法访问 phonegap API,例如 navigator.camera。
我怎样才能完成它以访问 API?
请不要评论它将被AppStore等拒绝。
感谢您的帮助!
最佳答案
嗯,对我来说,解决方案是多种来源的混合,但找到了大部分解决方案 here .
你应该做的是:
定义您的
config.xml
以直接指向远程index.html
。<content src="http://your-remote-location/index.html" />
在您的
index.html
中,任何对本地 android 设备资源的引用都带有一些独特的前缀,例如**injection**
。例如对于cordova.js
你会想出类似的东西:<script type="text/javascript" src="**injection**www/cordova.js"></script>
在以下位置找到
SystemWebViewClient.java
:your-project-location\platforms\android\CordovaLib\src\org\apache\cordova\engine
.在顶部类的私有(private)成员部分添加以下枚举声明:
private enum WebExtension { PNG, MP3, MP4, TTF, SVG, JS, ICO, HTML, CSS, EOT, WOFF, JSON; }
找到
shouldInterceptRequest
方法并在try {
行之后添加以下内容:if(url != null && url.contains(INJECTION_TOKEN)) { String assetPath = url.substring(url.indexOf(INJECTION_TOKEN) + INJECTION_TOKEN.length(), url.length()); try { String mimeType = "text/plain"; String ext = assetPath.substring(assetPath.lastIndexOf(".") + 1, assetPath.length()); WebExtension extension = WebExtension.valueOf(ext.toUpperCase()); switch(extension) { case PNG: mimeType = "image/png"; break; case MP3: mimeType = "audio/mpeg"; break; case MP4: mimeType = "video/mp4"; break; case TTF: mimeType = "application/x-font-ttf"; break; case SVG: mimeType = "image/svg+xml"; break; case JS: mimeType = "application/javascript"; break; case ICO: mimeType = "image/x-icon"; break; case HTML: mimeType = "text/html"; break; case CSS: mimeType = "text/css"; break; case EOT: mimeType = "application/vnd.ms-fontobject"; break; case WOFF: mimeType = "application/x-font-woff"; break; case JSON: mimeType = "application/json"; break; } WebResourceResponse response = new WebResourceResponse( mimeType, "UTF-8", parentEngine.webView.getContext().getAssets().open(assetPath) ); return response; } catch (IOException e) { e.printStackTrace(); // Failed to load asset file } }
所有的结果都是拦截每一个资源请求,如果其中包含 **injection**
字符串,它将削减资源位置并请求它从运行应用程序的本地设备位置。 mimeType 是应用浏览器以正确方式加载资源所必需的。
希望对某人有所帮助。
关于javascript - 如何从远程页面访问 phonegap API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21984688/