XWIKO
XWIKO

Reputation: 343

Cordova screenshot not working on time $IonicView.Enter

We are working on an iOS project in Ionic. We want the cordova screenshot plugin to fire on ionicview enter (when first entering the app), and then use cordova file transfer to send the screenshot.

The below code does not work when first time entering the view. When we leave the view and come back however, it does send a screenshot.

However, the first logAction DOES fire on the first time entering this view while the second logAction does not. When entering this view for the second time, both logActions are fired.

This is the part of the code i am referring to:

$scope.$on('$ionicView.enter', function () {
    $scope.logAction({
        "Message": "Login screen succesfully entered",
        "Succeeded": true,
        "transaction": 1,
        "Category": "Info",
        "Device": 0,
    })
  $cordovaScreenshot.capture().then(function(filepath){
    $scope.logAction({
      "Message": filepath,
      "Succeeded": true,
      "transaction": 1,
      "Category": "Info",
      "Device": 0,
    })
    $cordovaScreenshot.send(filepath);
  });
});

This is the cordovaScreenshot file

angular.module('testScreenshots.services', [])
.service('$cordovaScreenshot', ['$q',function ($q) {
    return {
        fileURL: "",
        capture: function (filename, extension, quality) {
          var randomNumber = Math.random();
          console.log("" + randomNumber);
          filename = "testPicture" + randomNumber.toString();
            extension = extension || 'jpg';
            quality = quality || '100';

            var defer = $q.defer();


            navigator.screenshot.save(function (error, res){
                if (error) {
                    console.error(error);
                    defer.reject(error);
                } else {
                    console.log('screenshot saved in: ', res.filePath);
                    this.fileURL = "file://" + res.filePath;
                    defer.resolve(res.filePath);
                    console.log("inside the save function: "+this.fileURL);
                }
            }, extension, quality, filename);

            return defer.promise;
        },


        send: function(filepath){

          var win = function (r) {
            console.log("Code = " + r.responseCode);
            console.log("Response = " + r.response);
            console.log("Sent = " + r.bytesSent);
          }

          var fail = function (error) {
            alert("An error has occurred: Code = " + error.code);
            console.log("upload error source " + error.source);
            console.log("upload error target " + error.target);
          }

          var options = new FileUploadOptions();
          options.fileKey = "file";
          options.fileName = filepath.substr(filepath.lastIndexOf('/') + 1);
          options.mimeType = "multipart/form-data";
          options.chunkedMode = false;
          options.headers = {
            Connection: "close"
          };

          var ft = new FileTransfer();
          ft.upload(filepath, encodeURI("http://192.168.6.165:8080//api/uploadpicture"), win, fail, options);
        }
    };
}])

Upvotes: 1

Views: 228

Answers (1)

XWIKO
XWIKO

Reputation: 343

The iOS simulator we use on the Mac had this issue. After trying to run it on a device the issue no longer affected us.

This issue thus seems to relate to using the iOS simulator.

Upvotes: 0

Related Questions