我如何判断我的 PhoneGap JavaScript 应用程序当前是在 Android 还是 iOS 上运行?
How can I tell if my PhoneGap JavaScript app currently runs on Android or iOS?
标题说明了一切:假设我有一个 PhoneGap 应用程序,是为 Android 和 iOS 构建的。用户在 his/her 移动设备 phone 上运行我的应用程序。在某些时候,如果它是 Android 或 iPhone,我想给出不同的消息...如何在我的 JavaScript 代码中检测到它?
另外:如果没有额外的 PhoneGap 插件会要求用户额外权限,我可以这样做吗?
如果你想要设备平台,你可以使用下面的代码。
var deviceType = (navigator.userAgent.match(/iPad/i)) == "iPad" ? "iPad" : (navigator.userAgent.match(/iPhone/i)) == "iPhone" ? "iPhone" : (navigator.userAgent.match(/Android/i)) == "Android" ? "Android" : (navigator.userAgent.match(/BlackBerry/i)) == "BlackBerry" ? "BlackBerry" : "null";
alert(deviceType);
或者根据您可以使用的新变化
// Depending on the device, a few examples are:
// - "Android"
// - "BlackBerry"
// - "iOS"
// - "webOS"
// - "WinCE"
// - "Tizen"
// - "browser"
var devicePlatform = device.platform;
devicePlatform 将 return 您提到的设备类型字符串。
它不需要任何类型的插件或许可。
像这样使用检测到的设备类型
if (/(android)/i.test(navigator.userAgent)) { // for android & amazon-fireos
alert("Android ");
} else if (/(ipod|iphone|ipad)/i.test(navigator.userAgent)) { // for ios
alert("IOS");
}
标题说明了一切:假设我有一个 PhoneGap 应用程序,是为 Android 和 iOS 构建的。用户在 his/her 移动设备 phone 上运行我的应用程序。在某些时候,如果它是 Android 或 iPhone,我想给出不同的消息...如何在我的 JavaScript 代码中检测到它?
另外:如果没有额外的 PhoneGap 插件会要求用户额外权限,我可以这样做吗?
如果你想要设备平台,你可以使用下面的代码。
var deviceType = (navigator.userAgent.match(/iPad/i)) == "iPad" ? "iPad" : (navigator.userAgent.match(/iPhone/i)) == "iPhone" ? "iPhone" : (navigator.userAgent.match(/Android/i)) == "Android" ? "Android" : (navigator.userAgent.match(/BlackBerry/i)) == "BlackBerry" ? "BlackBerry" : "null";
alert(deviceType);
或者根据您可以使用的新变化
// Depending on the device, a few examples are:
// - "Android"
// - "BlackBerry"
// - "iOS"
// - "webOS"
// - "WinCE"
// - "Tizen"
// - "browser"
var devicePlatform = device.platform;
devicePlatform 将 return 您提到的设备类型字符串。 它不需要任何类型的插件或许可。
像这样使用检测到的设备类型
if (/(android)/i.test(navigator.userAgent)) { // for android & amazon-fireos
alert("Android ");
} else if (/(ipod|iphone|ipad)/i.test(navigator.userAgent)) { // for ios
alert("IOS");
}