Created
May 18, 2017 09:40
-
-
Save 0x62/56d6b70aec2d832cf35e4ed01892a590 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
return new Promise((resolve, reject) => { | |
// Get current state (required, otherwise requestLocationAuthorization | |
// callback is triggered before change) | |
cordova.plugins.diagnostic.getLocationAuthorizationStatus(state => { | |
let res = validateState(state); | |
if (res.code == 0) { | |
return resolve(); | |
} else if (res.code == 1) { | |
return reject(res); | |
} else if (res.code == 2) { | |
triggerPermissionPrompt(); | |
} | |
}, err); | |
function triggerPermissionPrompt() { | |
cordova.plugins.diagnostic.requestLocationAuthorization(state => { | |
let res = validateState(state); | |
// At this point, if it's anything other than | |
// granted, user must change manually | |
if (res.code == 0) { | |
return resolve(); | |
} else { | |
return reject(res); | |
} | |
}, err, cordova.plugins.diagnostic.locationAuthorizationMode.ALWAYS); | |
} | |
function err(msg) { | |
console.log(`Error requesting location: ${msg}`); | |
return reject({ code: 3 }); | |
} | |
// Check value of custom constants | |
// 0: permission granted, resolve | |
// 1: permission must be manually changed, reject | |
// 2: not requested yet | |
function validateState(state) { | |
switch(state) { | |
case cordova.plugins.diagnostic.permissionStatus.GRANTED: | |
return { code: 0 } | |
case cordova.plugins.diagnostic.permissionStatus.DENIED: | |
case cordova.plugins.diagnostic.permissionStatus.GRANTED_WHEN_IN_USE: | |
return { code: 1 } | |
case cordova.plugins.diagnostic.permissionStatus.NOT_REQUESTED: | |
return { code: 2 } | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment