Created
December 6, 2013 18:21
-
-
Save digitarald/7829694 to your computer and use it in GitHub Desktop.
Using mozMobileConnections with MNC and MCC to enable app features for specific country/carrier combinations
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
var mccs = []; | |
try { | |
// navigator.mozMobileConnections is the new API. | |
// navigator.mozMobileConnection is the legacy API. | |
var conn; | |
if ((conn = navigator.mozMobileConnection)) { | |
console.log('navigator.mozMobileConnection available'); | |
// `MCC`: Mobile Country Code | |
// `MNC`: Mobile Network Code | |
// `lastKnownHomeNetwork`: `{MCC}-{MNC}` (SIM's origin) | |
// `lastKnownNetwork`: `{MCC}-{MNC}` (could be different network if roaming) | |
var network = (conn.lastKnownHomeNetwork || conn.lastKnownNetwork || '-').split('-'); | |
mcc = network[0]; | |
mnc = network[1]; | |
console.log('MCC = ' + mcc + ', MNC = ' + mnc); | |
mccs.push([mcc, mnc]); | |
} else if ((conn = navigator.mozMobileConnections)) { | |
console.log('navigator.mozMobileConnections available'); | |
for (var i = 0; i < conn.length; i++) { | |
var connData = conn[i].data; | |
if (connData && connData.network) { | |
mccs.push({ | |
mcc: connData.network.mcc, | |
mnc: connData.network.mnc | |
}); | |
} | |
} | |
} | |
} catch (e) { | |
// Fail gracefully if `navigator.mozMobileConnection` gives us problems. | |
console.warn('Error accessing navigator.mozMobileConnection:', e); | |
} | |
var enableApp = false; | |
if (mccs.length) { | |
// Look up region and carrier from MCC (Mobile Country Code) | |
// and MNC (Mobile Network Code). | |
// Codes from https://gist.github.com/digitarald/5717512 | |
// Make your own list, mcc as index and mncs as array | |
var enabledMccs = { | |
'732': [123], // Colombia | |
'734': [4], // Venezuela | |
'214': [5, 7], // Spain | |
'724': [6, 10, 11, 23] // Brazil | |
}; | |
// First returned "true" will stop .some iteration | |
enableApp = mccs.some(function(mccData) { | |
// Country is in list | |
var mncs = enabledMccs[mccData.mcc]; | |
// Carrier is in country list | |
return mncs && (mncs.indexOf(mccData.mnc) >= 0); | |
}); | |
} | |
if (enableApp) { | |
App.start(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment