【发布时间】:2018-05-27 20:28:24
【问题描述】:
我正在使用 Phonegap v6 构建一个应用程序,我正在使用 cordova 相机插件将图片上传到服务器,并且我已经正确设置了所有内容。
当我在 Phonegap Developer iOS App 中测试它可以工作时,我可以选择和图像并上传它。
但是当我编译 IPA 文件并将其安装在我的 iPhone 上时,当我点击选择图片按钮时,没有任何反应,但是当我点击它并按下任何表单输入后,画廊将打开,我可以选择一个图像,但图像不会被上传。完全没有反馈。
它同样适用于 Phonegap Mobile 应用,但不适用于独立 IPA。
这里是JS函数:
// take picture from camera
$('#but_take').click(function(){
navigator.camera.getPicture(onSuccess, onFail, { quality: 20,
destinationType: Camera.DestinationType.FILE_URL
});
});
// upload select
$("#but_select").click(function(){
navigator.camera.getPicture(onSuccess, onFail, { quality: 50,
sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
allowEdit: true,
destinationType: Camera.DestinationType.FILE_URI
});
});
// Change image source and upload photo to server
function onSuccess(imageURI) {
// Set image source
var image = document.getElementById('img');
image.src = imageURI + '?' + Math.random();
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1);
options.mimeType = "image/jpeg";
var params = {};
params.value1 = localStorage.phone;
params.value2 = "param";
options.params = params;
options.chunkedMode = false;
var ft = new FileTransfer();
ft.upload(imageURI,
"hiddenUrlHere", function(result){
myApp.alert(result.response, "Profile Updated");
}, function(error){
myApp.alert(JSON.stringify(error), "Error");
}, options);
}
function onFail(message) {
myApp.alert(message, "Error");
}
HTML sn-p:
<img src="img/cam2.jpg" id='img' style="width: 100px; height:
100px;">
<button id='but_select'>Select photo from Gallery</button>
config.xml 文件:
<?xml version='1.0' encoding='utf-8'?>
<widget id="appIDhere" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>AppName</name>
<description>
Senior Project By name
</description>
<author email="dev@cordova.apache.org" href="http://cordova.io">
name here
</author>
<content src="index.html" />
<access origin="*" />
<access origin="content:///*" />
<allow-navigation href="*" />
<allow-intent href="*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
<platform name="android">
<preference name="android-minSdkVersion" value="14" />
<allow-intent href="market:*" />
<feature name="Camera">
<param name="android-package" value="org.apache.cordova.camera.CameraLauncher"/>
</feature>
</platform>
<platform name="ios">
<access origin="*" />
<access origin="content:///*" />
<allow-navigation href="*" />
<allow-intent href="*" />
<allow-intent href="itms:*" />
<allow-intent href="itms-apps:*" />
<preference name="BackupWebStorage" value="none" />
<edit-config target="NSPhotoLibraryAddUsageDescription" file="*-Info.plist" mode="merge">
<string>need to photo library access to upload pictures for posts</string>
</edit-config>
<feature name="StatusBar">
<param name="ios-package" onload="true" value="CDVStatusBar" />
</feature>
<feature name="Camera">
<param name="ios-package" value="CDVCamera" />
</feature>
<feature name="Keyboard">
<param name="ios-package" onload="true" value="CDVKeyboard" />
</feature>
</platform>
<preference name="DisallowOverscroll" value="true" />
<plugin name="cordova-plugin-console" spec="~1.0.1" />
<plugin name="cordova-plugin-statusbar" spec="~1.0.1" />
<plugin name="phonegap-plugin-push" source="npm" spec="~1.8.0">
<param name="SENDER_ID" value="981753167590" />
</plugin>
<plugin name="cordova-plugin-camera" spec="https://github.com/apache/cordova-plugin-camera" />
<plugin name="cordova-plugin-keyboard" spec="https://github.com/cjpearson/cordova-plugin-keyboard" />
<plugin name="cordova-plugin-whitelist" spec="~1.3.3" />
</widget>
内容安全元标记:
<meta http-equiv="Content-Security-Policy" content="default-src *;font-src 'self' data:; style-src * 'unsafe-inline'; script-src * 'unsafe-inline' 'unsafe-eval'; media-src *; img-src * filesystem: data:">
同样,所有插件都已安装并正确链接,Phonegap iOS 应用程序中的一切都运行良好,但在独立 IPA 中却没有。
我有什么遗漏吗? 也许是一些配置,不是 Javascript 编码,也许是权限..
顺便说一句,Android APK 也在做同样的事情。
【问题讨论】:
-
您的 index.html 中是否有 Content-Security-Policy 元标记?如果是这样,请将其添加到问题中。顺便说一句,您没有安装文件传输插件
-
@jcesarmobile 是的,我现在添加元标记,不,还没有安装文件传输插件。
标签: javascript ios cordova phonegap-plugins phonegap