Phonegap相机插件无法在iOS

时间:2017-12-13 20:32:17

标签: javascript ios cordova phonegap-plugins phonegap

我正在使用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代码段:

<img src="img/cam2.jpg" id='img' style="width: 100px; height: 
100px;">

<button id='but_select'>Select photo from Gallery</button>

config.xml文件:

&#13;
&#13;
<?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>
&#13;
&#13;
&#13;

内容安全元标记:

 <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也在做同样的事情。

1 个答案:

答案 0 :(得分:1)

对于相机问题,您必须将class Base { }; template <typename T> class ClassTmplt: public Base { public: using InternalType = T; }; class DerivedFloat: public ClassTmplt<float> { }; template <typename T, typename TIn> void DoSomething(const TIn value) { T val = T(value); std::cout << val << " | " << typeid(val).name() << " | " << typeid(value).name() << std::endl; } template <typename T> typename T::InternalType PassInternalType(T* obj) { return T::InternalType; //return obj::InternalType } void test() { Base* obj = new DerivedFloat; DoSomething<float>(1.23); DoSomething<DerivedFloat::InternalType>(1UL); //DoSomething<(*obj)::InternalType>(-123); //DoSomething<PassInternalType(obj)>(4.56); } 添加到gap:元标记的default-src

实施例: Content-Security-Policy

对于上传问题,您必须安装cordova-plugin-file-transfer。

在Phonegap开发者应用上,它可以运行,因为它包含所有插件,并且在<meta http-equiv="Content-Security-Policy" content="default-src * gap:;font-src 'self' data:; style-src * 'unsafe-inline'; script-src * 'unsafe-inline' 'unsafe-eval'; media-src *; img-src * filesystem: data:"> 元标记上也有gap:

如果您想查看错误,可以在设备中启用远程调试,以便检查应用程序并查看桌面Safari上的错误。