Closed WebDevBren closed 6 years ago
I think you might be looking in the wrong location. Because the amountOfTimesFired
is only 1 in both instances, and it declared is outside the request permissions. This tells me that whatever is calling the beginning of your code is what is looping. Otherwise if the request permissions was causing the loop that counter would be 2.
I bet if you add a console.log("Hi");
where you have the let amountOfTimesFired = 0;
you would see "Hi" twice also...
the very first console.log (outputs : JS: Requesting Location Permissions
) is outside of the promise chain and only logs once.
also, i should mention that this behaviour only occurs when the application does not already have the permissions. if the user has given the permissions (either by accepting them, or via the application settings) it works as expected.
Sorry, I totally forgot about this bug report until I was cleaning up issues.
I took your code above and modified it to be only talking to this plugin and I cannot duplicate your issue:
var hasLocationPermissionsI = false;
var hasCameraPermissionsI = false;
var amountOfTimesFired = 0;
viewModel.testTap = function() {
var that = this;
console.log('Requesting Location Permissions');
permissions.requestPermission(android.Manifest.permission.ACCESS_FINE_LOCATION, "I need Location!")
.then(function (isLocationEnabled) {
hasLocationPermissionsI = isLocationEnabled;
console.log('Requesting Camera Permissions');
return permissions.requestPermission(android.Manifest.permission.CAMERA, "I need Camera!");
}).catch(function(err) {
hasLocationPermissionsI = false;
return permissions.requestPermission(android.Manifest.permission.CAMERA, "I need Camera!");
})
.then(function (isCameraEnabled) {
hasCameraPermissionsI = isCameraEnabled;
return Promise.resolve([hasLocationPermissionsI, hasCameraPermissionsI]);
}).catch (function (err) {
hasCameraPermissionsI = false;
return Promise.resolve([hasLocationPermissionsI, hasCameraPermissionsI]);
})
.then(function (val) {
var hasLocationPermissions = val[0], hasCameraPermissions = val[1];
console.log(" hasLocationPermission: ", hasLocationPermissions ? 'Yes' : 'No');
console.log(" hasCameraPermissions: ", hasCameraPermissions ? 'Yes' : 'No');
amountOfTimesFired++;
console.log("Amount of Times this has been fired: ", amountOfTimesFired);
if (hasLocationPermissions && hasCameraPermissions) {
that.set("message", "WooHoo you granted me all the permissions!");
} else {
that.set("message", "I need moooar permissions!");
}
})
.catch(function(err) {
console.log("Error", err);
});
};
I put this into the demo app; and added a button that fires this. I was unable to get a promise fired more than once. I randomly choose allow or deny; then on fresh runs I would reset the permissions to be unset for the app so it would be a fresh start.
My last time I ran it; I did Deny/Deny, then re-clicked the test button and did Allow/Allow. and it outputted:
JS: Requesting Location Permissions
JS: Requesting Camera Permissions
JS: hasLocationPermission: No
JS: hasCameraPermissions: No
JS: Amount of Times this has been fired: 1
JS: Requesting Location Permissions
JS: Requesting Camera Permissions
JS: hasLocationPermission: Yes
JS: hasCameraPermissions: Yes
JS: Amount of Times this has been fired: 2
This was done on an Android 7 emulator.
I have written a plugin that basically acts as a thin wrap around your plugin and adds IOS Permission requesting... (while attempting to keep a consistent API between the two, so some 'vanity' methods have been added)
you can see it here: https://www.npmjs.com/package/@spartadigital/nativescript-permissions
we've noticed some strange behaviour from the promises, it seems that they are being fired twice, once when the dialog is opened and once when the permission is granted/rejected, and i believe that the issue is with your plugin
Here is the implementation script
which results in the following log
Any Ideas?