使用 Phonegap 构建时,cordova-plugin-whitelist 不起作用

Posted

技术标签:

【中文标题】使用 Phonegap 构建时,cordova-plugin-whitelist 不起作用【英文标题】:cordova-plugin-whitelist not working when using Phonegap build 【发布时间】:2018-04-05 18:57:44 【问题描述】:

我遇到了一个问题,即白名单插件不适用于 android,这会导致每次 AJAX 调用都返回 404,这会破坏我的应用程序,因为我无法连接到我的后端服务器。 ios 工作正常,因为它不需要这个插件,如果我检查 http://build.phonegap.com 中的插件列表,它确实出现了,这里会发生什么?

这是我的config.xml(这个项目顺便用了XDK,所以我的config.xml就是用它构建的:

<?xml version="1.0" encoding="UTF-8" ?>
<widget xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0" xmlns:cdv="http://cordova.apache.org/ns/1.0" xmlns:android="http://schemas.android.com/apk/res/android" android-versionCode="7" ios-CFBundleVersion="7" version="1.0.7" id="com.project.app"  >
<!-- This file was generated by the Intel XDK Cordova Package Build tool. -->
<name>PROJECT NAME</name>
<description>PROJECT DESCRIPTION</description>
<author>COMPANY</author>
<content src="index.html"/>
<preference name="android-build-tool" value="gradle" />
<platform name="android" >
    <allow-intent href="tel:*"/>
    <allow-intent href="geo:*"/>
    <allow-intent href="mailto:*"/>
    <allow-intent href="http://app.project.com/"/>
    <allow-intent href="maps:*"/>
    <allow-intent href="http://maps.google.com/"/>
    <access origin="*"/>
    <access origin="http://app.project.com/"/>
    <access origin="tel:*"/>
</platform>
<platform name="ios" >
    <allow-intent href="tel:*"/>
    <allow-intent href="geo:*"/>
    <allow-intent href="mailto:*"/>
    <allow-intent href="http://app.project.com/"/>
    <allow-intent href="https://www.google.com/maps"/>
    <allow-intent href="https://*.google.*"/>
    <access origin="*"/>
    <access origin="http://app.project.com/"/>
    <access origin="tel:*"/>
</platform>
<preference name="phonegap-version" value="cli-6.5.0" />
<preference name="xwalkMultipleApk" value="false" />
<plugin name="cordova-plugin-device" spec="1.1.6"/>
<plugin name="ionic-plugin-keyboard" spec="2.2.1"/>
<plugin name="phonegap-plugin-push" spec="1.4.3"/>
<plugin name="cordova-plugin-inappbrowser" spec="1.7.1"/>
<plugin name="cordova-plugin-splashscreen" spec="4.0.3"/>
<plugin name="cordova-plugin-statusbar" spec="2.2.3"/>
<plugin name="cordova-plugin-whitelist" spec="https://github.com/apache/cordova-plugin-whitelist"/> <!-- If available via Cordova registry; consider changing to an NPM reference. -->
<platform name="android" >
    <preference name="android-minSdkVersion" value="16"/>
    <preference name="android-targetSdkVersion" value="23"/>
    <preference name="android-installLocation" value="auto"/>
    <preference name="android-signed" value="true"/>
    <preference name="AndroidLaunchMode" value="singleTop"/>
</platform>
<platform name="ios" >
    <preference name="deployment-target" value="9.0"/>
    <preference name="ios-configuration" value="production"/>
</platform>
<splash platform="android" src="package-assets/splashs/ANDROID/320x426.png" density="ldpi"   orientation="portrait"/>
<splash platform="android" src="package-assets/splashs/ANDROID/320x470.png" density="mdpi"   orientation="portrait"/>
<splash platform="android" src="package-assets/splashs/ANDROID/480x640.png" density="hdpi"   orientation="portrait"/>
<splash platform="android" src="package-assets/splashs/ANDROID/720x960.png" density="xhdpi"   orientation="portrait"/>
<splash platform="ios" src="package-assets/splashs/ios/640x960.png"   orientation="portrait"/>
<splash platform="ios" src="package-assets/splashs/ios/640x1136.png"   orientation="portrait"/>
<splash platform="ios" src="package-assets/splashs/ios/750x1334.png"   orientation="portrait"/>
<splash platform="ios" src="package-assets/splashs/ios/768x1024.png"   orientation="portrait"/>
<splash platform="ios" src="package-assets/splashs/ios/1242x2208.png"   orientation="portrait"/>
<splash platform="ios" src="package-assets/splashs/ios/1536x2048.png"   orientation="portrait"/>
<splash platform="ios" src="package-assets/splashs/ios/2048x2732.png"   orientation="portrait"/>
<icon platform="android" src="package-assets/iconos/android/project48.png" density="mdpi"  />
<icon platform="android" src="package-assets/iconos/android/project72.png" density="hdpi"  />
<icon platform="android" src="package-assets/iconos/android/project96.png" density="xhdpi"  />
<icon platform="ios" src="package-assets/iconos/ios/project29.png"  />
<icon platform="ios" src="package-assets/iconos/ios/project58.png"  />
<icon platform="ios" src="package-assets/iconos/ios/project87.png"  />
<icon platform="ios" src="package-assets/iconos/ios/project60.png"  />
<icon platform="ios" src="package-assets/iconos/ios/project120.png"  />
<icon platform="ios" src="package-assets/iconos/ios/project180.png"  />
<icon platform="ios" src="package-assets/iconos/ios/project76.png"  />
<icon platform="ios" src="package-assets/iconos/ios/project152.png"  />
<icon platform="ios" src="package-assets/iconos/ios/project80.png"  />
<icon platform="ios" src="package-assets/iconos/ios/project57.png"  />
<icon platform="ios" src="package-assets/iconos/ios/project114.png"  />
<icon platform="ios" src="package-assets/iconos/ios/project72.png"  />
<icon platform="ios" src="package-assets/iconos/ios/project144.png"  />
<icon platform="ios" src="package-assets/iconos/ios/project50.png"  />
<icon platform="ios" src="package-assets/iconos/ios/project100.png"  />
<icon platform="ios" src="package-assets/iconos/ios/project187.png"  />
<config-file platform="android" parent="/manifest/application" mode="merge">
    <activity android:launchMode="singleTop" />
</config-file>
</widget>

【问题讨论】:

【参考方案1】:

我建议从敞开的一切开始——删除你所有的 allow-intentaccess 声明,只保留这个:

<access origin="*" />

并且不要在任何&lt;platform /&gt; 标记中包含它。从那里向后移动,将其限制为仅您想要的域。

此外,我没有看到任何提及您的Content Security Policy。根据文档,您也应该在 html 文件中设置它。再一次,从敞开的开始,我认为它看起来像这样:

<meta http-equiv="Content-Security-Policy" content="default-src 'self' *" />

【讨论】:

【参考方案2】:

在你的 config.xml 你必须添加:

<gap:plugin name="cordova-plugin-whitelist" source="npm" />

<plugin name="cordova-plugin-whitelist" spec="1.3.4" />

【讨论】:

【参考方案3】:

也许把这段代码放在 config.xml 中

<access origin="https://*"/>
<access origin="http://*"/>
<allow-navigation href="http://*"/>
<allow-navigation href="https://*"/>

【讨论】:

你能写一些解释,让OP可以理解答案吗? 我应该在Android的允许中添加这个吗?还是应该是“一般”(如***别的标签)? ponelo aca dentro en vez de lo que está actualmente 我已经尝试过了,但我又做了一次以确保它似乎没有工作,我仍然从每个电话中得到 404。我觉得插件本身在构建 APK 时没有正确安装,但我似乎找不到原因

以上是关于使用 Phonegap 构建时,cordova-plugin-whitelist 不起作用的主要内容,如果未能解决你的问题,请参考以下文章

xcode 4 + phonegap ...在构建时不更新JS?

如何防止meteor.local的负载(使用phonegap构建ios应用程序时)

使用 PhoneGap/Cordova 为 Android 构建时,在 Mac OS X 10.9 Mavericks 上执行命令“ant”时出错

使用 Phonegap 构建时,cordova-plugin-whitelist 不起作用

Phonegap 构建:“无法复制 cordova.js”

如何使用 Cordova 插件进行 PhoneGap 构建