xerotolerant xerotolerant - 1 month ago 31
iOS Question

iOS photo album permissions nativescript

Hello I am attempting to access the user photo album on iOS in order for users to select a profile pic in my app.

I am using native script and I cannot figure out how to request permission to do so. My error is:

CONSOLE LOG file:///app/shared/user-view-model/user-view-model.js:96:28: fileUri: file:///var/mobile/Media/DCIM/100APPLE/IMG_0002.JPG
CONSOLE LOG file:///app/shared/user-view-model/user-view-model.js:101:24: NSErrorWrapper: You don’t have permission to save the file “100APPLE” in the folder “DCIM”.


I used

<key>NSPhotoLibraryUsageDescription</key>
<string> ${PRODUCT_NAME} photo use</string>


in my info.plist but no luck. Does anybody know what I'm missing?

var frame = require("ui/frame");
var platform = require("platform");
var firebase = require("nativescript-plugin-firebase");
var page;
var list;

function pageLoaded(args) {
page = args.object;
list = page.getViewById("urls-list");
}
exports.pageLoaded = pageLoaded;

function onSelectMultipleTap(args) {
var imagepicker = require("nativescript-imagepicker");
var context = imagepicker.create({
mode: "multiple"
});
startSelection(context);
}
exports.onSelectMultipleTap = onSelectMultipleTap;

function onSelectSingleTap(args) {
var imagepicker = require("nativescript-imagepicker");
var context = imagepicker.create({
mode: "single"
});
startSelection(context);
}
exports.onSelectSingleTap = onSelectSingleTap;

function startSelection(context) {
context
.authorize()
.then(function() {
list.items = [];
return context.present();
})
.then(function(selection) {
console.log("Selection done:");
selection.forEach(function(selected) {
console.log("----------------");
console.log("uri: " + selected.uri);
console.log("fileUri: " + selected.fileUri);
uploadImage(selected.fileUri);
});
list.items = selection;
}).catch(function (e) {
console.log(e);
});
}

function uploadImage(imageUrl){
firebase.uploadFile({
remoteFullPath: "test/testimage.jpg",
localFullPath: imageUrl,
onProgress: function(status) {
console.log("Uploaded fraction: " + status.fractionCompleted);
console.log("Percentage complete: " + status.percentageCompleted);
}
}).then((success) => {
console.log("success: " + success);
}, (error) => {
console.log("Error: " + error);
})
}

Answer

I found a solution from this github post

https://github.com/NativeScript/sample-ImageUpload/issues/2#issuecomment-252795778

The solution was to use the getImage() method and then saveToFile method on the result.

private selectImage(context){
    context.authorize()
      .then(() => {return context.present()})
      .then((selection) => {
        selection.forEach((selected) => {
          selected.getImage()
            .then((imageSource) => {
              var appPath = fs.knownFolders.currentApp().path;
              var profilePath = "/img/profile.jpg";
              var filePath = fs.path.join(appPath, profilePath);
              var saved = imageSource.saveToFile(filePath, enums.ImageFormat.jpeg);
              console.log(`item saved:${saved}`);
              console.log("FilePath: " + filePath);
              console.log("Image source " + JSON.stringify(imageSource));
            },
            (error) => {
              console.log("error");
            })
        })
      })
  }
Comments