1

I want to upload my image from my photo gallery in ios simulator to my amzazon s3 bucket. This code works fine on android but refuses to work on iOS and im not sure why

code to select photo from image gallery

  $scope.choosePhoto = function(index) {
     var options = {
      destinationType : Camera.DestinationType.FILE_URL,
      sourceType : Camera.PictureSourceType.PHOTOLIBRARY,
      allowEdit : false,
      encodingType: Camera.EncodingType.JPEG,
      popoverOptions: CameraPopoverOptions,
      mediaType: Camera.MediaType.PICTURE,
      correctOrientation: true
    };

   // 3
    $cordovaCamera.getPicture(options).then(function(imageData) {
   // 4
      var imagetype; 
      onImageSuccess(imageData);
      function onImageSuccess(fileURI) {
        createFileEntry(fileURI);  
      }

      function createFileEntry(fileURI) {
        window.resolveLocalFileSystemURL(fileURI, copyFile, fail);
      }

       // 5
      function copyFile(fileEntry) {
        var name = fileEntry.fullPath.substr(fileEntry.fullPath.lastIndexOf('/') + 1);
        var newName = (new Date()).getTime() + name;
         halfthru(fileEntry, newName); //diff
         getImageType(fileEntry); //diff

      }

      function getImageType(fileEntry) { //diff
        var typeImage;
        $scope.$evalAsync(function() {
          fileEntry.file(function(file){
            typeImage= file.type;
            $scope.imagelist = typeImage;
            imagetype = typeImage;
          }, function(e){                                              
          }); 
        })

      }

     function halfthru(fileEntry, newName) {
        window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function(fileSystem2) {
          fileEntry.copyTo(
            fileSystem2,
            newName,
            onCopySuccess,
            fail
          );
        }, fail);   
      }

      // 6
      function onCopySuccess(entry) {  
          $scope.activeSlide = index;
            $scope.modal1.show();
            $scope.$evalAsync($scope.images.push({file: entry.nativeURL, type: $scope.imagelist}));
            imagesModalCount =  $scope.images.length;
            attachedImageCount  = $scope.imagesAttached.length;

          $scope.$on('$destroy', function() {
            $scope.modal1.remove();
          });

      }

      function fail(error) {

      }
    }, function(err) {

    });
  }

code to upload to amazon

$scope.uploadImage = function(imageURI, fileName) {
    var deferred = $q.defer();
      createCase.getAws().then(function(awsDetails) {
        var policy = awsDetails.policy;
        var signature = awsDetails.signature;
        var key = awsDetails.key;
        var datenow = awsDetails.datenow;
        var s3URI = encodeURI("https://partners-mobile-app.s3.amazonaws.com/"),
        awsKey = key,
        policyBase64 = policy,
        acl = "public-read";  
          var ft = new FileTransfer();
          var options = new FileUploadOptions();
          options.fileKey = "file";
          options.fileName = fileName;
          options.mimeType = "image/jpeg";
          options.chunkedMode = false;
          options.headers = {
            Connection: "close"
          };
          options.params = {
              "key": fileName,
              "AWSAccessKeyId": key,
              "acl": acl,
              "policy": policyBase64,
              "signature": signature,
              "Content-Type": "image/jpeg"
          };


          var f =  ft.upload(imageURI, s3URI,
            function (e) {
              console.log("uploadimage: "+ imageURI)
               console.log("uploads3uri: "+ s3URI)
              console.log("im in upload now")
              $scope.finalimagelist.push(s3URI+fileName);
              if($scope.finalimagelist.length === $scope.images.length){
                console.log("OK SER GO")
                deferred.resolve($scope.finalimagelist);
              }    
            },
            function (e) {
              deferred.reject(e);
            }, 
          options);
        }

SO basically, i choose the image from image gallery and then upload to amazon everythg looks good but it just doesnt upload the image and i dont know why.

I am using AngularJS and the plugin cordova-plugin-file-transfer

my info.plist

<key>NSAppTransportSecurity</key>
    <dict>
      <key>NSAllowsArbitraryLoads</key>
      <true/>
    </dict>
Kingsley Simon
  • 2,090
  • 5
  • 38
  • 84

1 Answers1

0

Please have a look to this. This whole thing sounds to me like a problem with ATS / Apple Transport Security on iOS 9

This would be the solution for it: https://stackoverflow.com/a/32710127/3671726

Community
  • 1
  • 1
Sithys
  • 3,655
  • 8
  • 32
  • 67
  • i already have the NSTransportSecurity added in my plist file. so i dont think that the problem – Kingsley Simon Nov 06 '15 at 09:11
  • share your `info.plist` than please. – Sithys Nov 06 '15 at 10:08
  • and also your settings for whitelisting – Sithys Nov 06 '15 at 10:08
  • NSAppTransportSecurity NSAllowsArbitraryLoads – Kingsley Simon Nov 06 '15 at 11:12
  • idont think i have settings for whitelisting bcos each time in run the cordova plugin whitelist on xcode, it kept tellin me it has been deprecated – Kingsley Simon Nov 06 '15 at 11:13
  • i might be wrong but i dont think this is an issue wt whitelisting. I think it is a problem with the cordova-plugin-file-transfer itself – Kingsley Simon Nov 06 '15 at 11:19
  • which error do you get in console? You should read my link. The way you are sending or getting data from a server is not secure! – Sithys Nov 06 '15 at 12:27
  • yes i can do that but it still remains that the issue is not the transport security but the file transfer plugin or the file plugin – Kingsley Simon Nov 07 '15 at 03:01
  • i dont get any error in console. I have applied your suggestions in your link and its stil not working? Any other suggestions? I appreciate your suggestions thanks. – Kingsley Simon Nov 07 '15 at 17:57
  • [HOW TO apply the Cordova/Phonegap the whitelist system](https://github.com/jessemonroy650/top-phonegap-mistakes/blob/master/the-whitelist-system.md) Let me know, if anything here helps. –  Nov 08 '15 at 01:34