Skip to content

Instantly share code, notes, and snippets.

@Ayyagaries
Created October 20, 2020 15:24
Show Gist options
  • Save Ayyagaries/c75019ef9b99e2b1c1df2a744e0f443c to your computer and use it in GitHub Desktop.
Save Ayyagaries/c75019ef9b99e2b1c1df2a744e0f443c to your computer and use it in GitHub Desktop.
var dialogs = require('alloy/dialogs');
function editPermissions(e) {
if (OS_IOS) {
Ti.Platform.openURL(Ti.App.iOS.applicationOpenSettingsURL);
}
if (OS_ANDROID) {
var intent = Ti.Android.createIntent({
action: 'android.settings.APPLICATION_SETTINGS',
});
intent.addFlags(Ti.Android.FLAG_ACTIVITY_NEW_TASK);
Ti.Android.currentActivity.startActivity(intent);
}
}
function showDialog() {
var resp = dialogs.confirm({
title: 'You denied permission for camera',
message: 'SEC³URE requested permission for camera. Press Yes to open the App Settings to grant permission there.',
callback: editPermissions
});
}
exports.hasPermissions = function() {
// This is now the cross-platform way to check permissions.
// The above is still useful as it provides the reason of denial.
var hasCameraPermissions = Ti.Media.hasCameraPermissions();
return hasCameraPermissions;
};
exports.requestPermissions = function(callback) {
// On iOS we can get information on the reason why we might not have permission
if (OS_IOS) {
// Map constants to names
var map = {};
map[Ti.Media.CAMERA_AUTHORIZATION_AUTHORIZED] = 'CAMERA_AUTHORIZATION_AUTHORIZED';
map[Ti.Media.CAMERA_AUTHORIZATION_DENIED] = 'CAMERA_AUTHORIZATION_DENIED';
map[Ti.Media.CAMERA_AUTHORIZATION_RESTRICTED] = 'CAMERA_AUTHORIZATION_RESTRICTED';
map[Ti.Media.CAMERA_AUTHORIZATION_UNKNOWN] = 'CAMERA_AUTHORIZATION_UNKNOWN';
var cameraAuthorizationStatus = Ti.Media.cameraAuthorization;
if (cameraAuthorizationStatus === Ti.Media.CAMERA_AUTHORIZATION_RESTRICTED || cameraAuthorizationStatus === Ti.Media.CAMERA_AUTHORIZATION_DENIED) {
showDialog();
}
}
Ti.Media.requestCameraPermissions(function(e) {
if (e.success) {
callback(true);
} else {
showDialog();
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment