Cordova/jQuery - 确定您的应用程序是 运行 在 Web/Mobile 浏览器还是移动应用程序 (Android/iOS)
Cordova/jQuery - Identifying whether your app is being run on Web/Mobile browser or mobile app (Android/iOS)
我使用 Cordova/Phonegap 为 Android/iOS 开发应用程序。我通常为我的网络和移动内容使用单一代码库。我使用 SQLite 数据库 and/or 其他原生插件,当它是一个移动应用程序时,当我在网络上时必须避免这些 LOCs。
但我遇到了一个问题,无法确定我的应用程序是 运行 在桌面网络浏览器上/Mac/Android/iOS 还是移动应用程序 (Android/iOS).
我试过 userAgent 嗅探,但这种正则表达式技术失败了,尤其是当 运行在移动浏览器上运行代码时。以下是我用来识别 OS 和设备版本的代码:
getOSAndVersion: function() {
var that = this;
var userOS; // will either be iOS, Android or unknown
var userOSver; // this is a string, used to denote OS version
var ua = navigator.userAgent;
var uaindex;
if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(ua)) {
window.deviceType = "Mobile";
} else {
window.deviceType = "Web";
}
// determine OS
if (ua.match(/iPad/i) || ua.match(/iPhone/i)) {
userOS = 'iOS';
uaindex = ua.indexOf('OS ');
}
else if (ua.match(/Android/i)) {
userOS = 'Android';
uaindex = ua.indexOf('Android ');
}
else {
userOS = 'unknown';
}
// determine version
if (userOS === 'iOS' && uaindex > -1) {
userOSver = ua.substr(uaindex + 3, 3).replace('_', '.');
}
else if (userOS === 'Android' && uaindex > -1) {
userOSver = ua.substr(uaindex + 8, 3);
}
else {
userOSver = 'unknown';
}
return { osVersion: userOSver, os: userOS, deviceType: window.deviceType };
}
我是否可以使用任何其他技术来可靠地识别我的代码所在的位置 运行?
P.S。 : 我反对使用任何其他 Cordova/JS 插件来识别它,但仍然开放讨论。
在 Cordova 中,当应用程序运行到应用程序时,url 以 file://
为前缀,而当 运行 在移动浏览器中, url 以 [=11= 为前缀] 或 https
协议。
解决方案:
- 获取 url 你的当前页面 (check this)
- 如果 file:// 是其应用程序,则识别其前缀
- 如果是 http 或 https,则移动浏览器
您可以检查是否定义了 cordova
?
if (cordova) {
// Running in your app
} else {
// Not running in your app, so website
}
我使用 Cordova/Phonegap 为 Android/iOS 开发应用程序。我通常为我的网络和移动内容使用单一代码库。我使用 SQLite 数据库 and/or 其他原生插件,当它是一个移动应用程序时,当我在网络上时必须避免这些 LOCs。
但我遇到了一个问题,无法确定我的应用程序是 运行 在桌面网络浏览器上/Mac/Android/iOS 还是移动应用程序 (Android/iOS).
我试过 userAgent 嗅探,但这种正则表达式技术失败了,尤其是当 运行在移动浏览器上运行代码时。以下是我用来识别 OS 和设备版本的代码:
getOSAndVersion: function() {
var that = this;
var userOS; // will either be iOS, Android or unknown
var userOSver; // this is a string, used to denote OS version
var ua = navigator.userAgent;
var uaindex;
if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(ua)) {
window.deviceType = "Mobile";
} else {
window.deviceType = "Web";
}
// determine OS
if (ua.match(/iPad/i) || ua.match(/iPhone/i)) {
userOS = 'iOS';
uaindex = ua.indexOf('OS ');
}
else if (ua.match(/Android/i)) {
userOS = 'Android';
uaindex = ua.indexOf('Android ');
}
else {
userOS = 'unknown';
}
// determine version
if (userOS === 'iOS' && uaindex > -1) {
userOSver = ua.substr(uaindex + 3, 3).replace('_', '.');
}
else if (userOS === 'Android' && uaindex > -1) {
userOSver = ua.substr(uaindex + 8, 3);
}
else {
userOSver = 'unknown';
}
return { osVersion: userOSver, os: userOS, deviceType: window.deviceType };
}
我是否可以使用任何其他技术来可靠地识别我的代码所在的位置 运行?
P.S。 : 我反对使用任何其他 Cordova/JS 插件来识别它,但仍然开放讨论。
在 Cordova 中,当应用程序运行到应用程序时,url 以 file://
为前缀,而当 运行 在移动浏览器中, url 以 [=11= 为前缀] 或 https
协议。
解决方案:
- 获取 url 你的当前页面 (check this)
- 如果 file:// 是其应用程序,则识别其前缀
- 如果是 http 或 https,则移动浏览器
您可以检查是否定义了 cordova
?
if (cordova) {
// Running in your app
} else {
// Not running in your app, so website
}