javascript - 如何从远程页面访问 phonegap API

标签 javascript android ios cordova cordova-plugins

我必须以下情况: 我有一个已经存在的远程网页,我想开发一个使用这个页面的应用程序。 到目前为止,一切都很好。当我启动应用程序时,会加载本地 index.html 并将 (window.open target: _self) 重定向到外部网站。该网站在 phonegap webview 中打开。在外部网站上,我添加了 cordova.js 以访问 native phonegap API。但它不能正常工作。 deviceReady 事件被正确触发,但我无法访问 phonegap API,例如 navigator.camera。

我怎样才能完成它以访问 API?

请不要评论它将被AppStore等拒绝。

感谢您的帮助!

最佳答案

嗯,对我来说,解决方案是多种来源的混合,但找到了大部分解决方案 here .

你应该做的是:

  1. 定义您的 config.xml 以直接指向远程 index.html

    <content src="http://your-remote-location/index.html" />
    
  2. 在您的 index.html 中,任何对本地 android 设备资源的引用都带有一些独特的前缀,例如 **injection**。例如对于 cordova.js 你会想出类似的东西:

    <script type="text/javascript" src="**injection**www/cordova.js"></script>
    
  3. 在以下位置找到 SystemWebViewClient.java:your-project-location\platforms\android\CordovaLib\src\org\apache\cordova\engine .

  4. 在顶部类的私有(private)成员部分添加以下枚举声明:

    private enum WebExtension {
        PNG, MP3, MP4, TTF, SVG, JS, ICO, HTML, CSS, EOT, WOFF, JSON;
    }
    
  5. 找到 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/

相关文章:

javascript - 如何临时修改元素的CSS

android - 获取 "' 安卓 :icon' attribute: attribute is not a string value” error while uploading an APK to the play store

android - 将项目升级到 Android Studio 1.0(Gradle 问题)

ios - AVAudioPlayer 第一次不播放

javascript - JS工兵: posting data to server (the right way ?)

javascript - Jquery each through <a> 只给我 href

android - 在 Android 中将图像添加到 iText pdf

iphone - NSNotifications 有哪些类型?

ios - 如何填充在其行中嵌入了 collectionView 的 tableView 并按类型或类别组织它们?

javascript - 移动设备上的 CSS 下拉菜单。如何绕过 :hover