标题说明了一切:假设我有一个 PhoneGap 应用程序,同时适用于 Android 和 iOS。用户在他/她的手机上运行我的应用程序。在某些时候,如果是 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 将返回您提到的设备类型字符串。 它不需要任何类型的插件或许可。
关于javascript - 如何判断我的 PhoneGap JavaScript 应用程序当前是在 Android 还是 iOS 上运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47130765/