git Phonegap - 自动包含正确的cordova

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10923320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 07:02:32  来源:igfitidea点击:

Phonegap - automatically including correct cordova

javascriptandroidiosgitcordova

提问by Farzad A

I'm developing a phonegap app on both iOS and android and have my www directory version controlled with git. I know that my HTML file needs to include the correct Cordova.js file (depending on which platform I'm currently developing on).

我正在 iOS 和 android 上开发一个 phonegap 应用程序,并使用 git 控制我的 www 目录版本。我知道我的 HTML 文件需要包含正确的 Cordova.js 文件(取决于我目前正在开发的平台)。

It is a little annoying pulling changes to www on iOS when someone was working on android. It gives me the endless gap_poll alert.

当有人在 android 上工作时,在 iOS 上对 www 进行更改有点烦人。它给了我无尽的 gap_poll 警报。

Simple solution is to remember to change the Cordova.js src so it points to the iOS version again. The problem with that is the other developers will need to keep changing their src if the latest commit was done on another platform.

简单的解决方案是记住更改 Cordova.js src,使其再次指向 iOS 版本。问题是如果最新的提交是在另一个平台上完成的,其他开发人员将需要不断更改他们的 src。

Is there a way to automatically detect which version of Cordova to include? That way it would work on any platform and we wouldn't have to make tedious changes.

有没有办法自动检测要包含哪个版本的 Cordova?这样它就可以在任何平台上运行,我们就不必进行繁琐的更改。

采纳答案by codemonkey

I have the same setup with my shared HTML/JS as a Git sub-module. My index.html sits in the shared folder and includes the platform-specific JS files using a relative path.

我的共享 HTML/JS 设置与 Git 子模块相同。我的 index.html 位于共享文件夹中,并使用相对路径包含特定于平台的 JS 文件。

There are 3 Git projects - Android, iOS and Shared. Shared is added as a sub-module in Android and iOS.

有 3 个 Git 项目——Android、iOS 和 Shared。Shared 在 Android 和 iOS 中作为子模块添加。

Folder structure

文件夹结构

www
|-platform
    |-js/libs/cordova.js
|-shared
    |-index.html

And in index.html

在 index.html 中

<script type="text/javascript" src="../platform/js/libs/cordova.js"></script>

I use the same idea to include JS files for plugins which are also platform dependant.

我使用相同的想法为插件包含 JS 文件,这些插件也依赖于平台。

回答by dhaval

The following code works fine for me:

以下代码对我来说很好用:

    function init() {
        if(isAndroid()){
            $("script").attr("src", "lib/android/cordova-1.7.0.js").appendTo("head");
        }else if(isiOS()){
            $("script").attr("src", "lib/ios/cordova-1.7.0.js").appendTo("head");
        }

         document.addEventListener("deviceready", onDeviceReady, false);
    }

    function isAndroid(){
        return navigator.userAgent.indexOf("Android") > 0;
    }

    function isiOS(){
        return ( navigator.userAgent.indexOf("iPhone") > 0 || navigator.userAgent.indexOf("iPad") > 0 || navigator.userAgent.indexOf("iPod") > 0); 
    }

    function onDeviceReady(){
        console.log("device is ready");
    }

You can check the full source code here

您可以在此处查看完整的源代码

回答by drdan

You should only have your master WWW under source control, which shouldn't contain any cordovafiles. These should be added by phonegap to your platform folder when you do a phonegap build. So your common index.html can have:

您应该只将您的主 WWW 置于源代码管理之下,其中不应包含任何cordova文件。当您进行 phonegap 构建时,这些应该由 phonegap 添加到您的平台文件夹中。所以你常用的 index.html 可以有:

<script type="text/javascript" src="cordova.js"></script>

In your master WWW you wont have a cordova.js script, but you will do in your platform/android folder when you do a:

在您的主 WWW 中,您不会有cordova.js 脚本,但是当您执行以下操作时,您将在您的平台/android 文件夹中执行:

phonegap build android

If you do need to conditionally tweak the content in your platform/xyz folders Grunt.JS is a great option.

如果您确实需要有条件地调整 platform/xyz 文件夹中的内容,Grunt.JS 是一个不错的选择。

回答by Ted Avery

@dhaval's answer did not work for me in a Cordova application on iOS 7.1.1. Although the script tag was added to the header, the scripts would not actually load as I verified with Safari's remote inspector.

@dhaval 的回答在 iOS 7.1.1 上的 Cordova 应用程序中对我不起作用。尽管脚本标记已添加到标题中,但在我使用 Safari 的远程检查器进行验证时,脚本实际上不会加载。

Instead, I added the following script, which I grabbed from another SA answer.

相反,我添加了以下脚本,该脚本是从另一个 SA 答案中抓取的。

function getScript(src, func) {
    var script = document.createElement('script');
    script.async = "async";
    script.src = src;
    if (func) {
       script.onload = func;
    }
    document.getElementsByTagName("head")[0].appendChild( script );
}

and then

进而

if ( navigator.userAgent.indexOf("iPhone") > 0 || navigator.userAgent.indexOf("iPad") > 0 || navigator.userAgent.indexOf("iPod") > 0) {
  getScript("js/cordova_ios.js");
} else if (navigator.userAgent.indexOf("Android") > 0) {
  getScript("js/cordova_android.js");
}

document.addEventListener("deviceready", onDeviceReady, false);

回答by Svenbj?rn Kvilt Olsen

The above solution didn't work for me. So i added this jquery solution:

上述解决方案对我不起作用。所以我添加了这个 jquery 解决方案:

    var isAndroid = /android/i.test(navigator.userAgent.toLowerCase());

if (isAndroid)
{
var script = document.createElement('script');
script.src = 'cordova/android/cordova.js';
 document.head.appendChild(script);
}
var IOS = /ipad|iphone|ipod/i.test(navigator.userAgent.toLowerCase());
if (IOS)
{
 var script = document.createElement('script');
 script.src = 'cordova/ios/cordova.js';
 document.head.appendChild(script);
}

Same goes for other Phonegap platform, for windows phone just add /windows phone/ etc.

其他 Phonegap 平台也是如此,对于 windows 手机,只需添加 /windows phone/ 等。

And if you have plugins, which also can be different from platfrom to platform. Just add plugin folder and cordova_plugin.js to same folder.

如果你有插件,这也可能因平台而异。只需将插件文件夹和cordova_plugin.js 添加到同一文件夹中。

回答by Kiko Fernandez

In my case, I just created a zip file for the wwwfolder without the cordova.jsor cordova-1.7.0.js(or other version). I submitted that zip to the build process and it generates the right cordova.jsfile so that it works on iOs and Android.

就我而言,我只是为www文件夹创建了一个 zip 文件,没有cordova.jscordova-1.7.0.js(或其他版本)。我将该 zip 提交到构建过程,它会生成正确的cordova.js文件,以便它可以在 iOs 和 Android 上运行。

Tell me if it worked.

告诉我它是否有效。