cordova-plugin-device

Android Testsuite Chrome Testsuite iOS Testsuite Lint Test

這個外掛程式定義了一個全域的 device 物件,它描述了裝置的硬體和軟體。雖然這個物件在全域範圍中,但它在 deviceready 事件之後才會可用。

document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
    console.log(device.cordova);
}

安裝

cordova plugin add cordova-plugin-device

屬性

  • device.cordova
  • device.model
  • device.platform
  • device.uuid
  • device.version
  • device.manufacturer
  • device.isVirtual
  • device.serial
  • device.sdkVersion (僅限 Android)

device.cordova

傳回應用程式中捆綁的 Cordova 平台版本。

版本資訊來自 cordova.js 檔案。

此屬性不會顯示其他已安裝平台的版本資訊。僅會顯示各自正在執行的平台的版本。

範例

如果 Cordova Android 10.1.1 安裝在 Cordova 專案中,則在 Android 應用程式中的 cordova.js 檔案將包含 10.1.1

device.cordova 屬性將顯示 10.1.1

支援平台

  • Android
  • 瀏覽器
  • iOS
  • Windows
  • OS X

device.model

device.model 會傳回裝置的型號或產品名稱。該值由裝置製造商設定,並且在相同產品的不同版本之間可能會有所不同。

支援平台

  • Android
  • 瀏覽器
  • iOS
  • Windows
  • OS X

快速範例

// Android: Pixel 4             returns "Pixel 4"
//          Motorola Moto G3    returns "MotoG3"
// Browser: Google Chrome       returns "Chrome"
//          Safari              returns "Safari"
// iOS:     iPad Mini           returns "iPad2,5"
//          iPhone 5            returns "iPhone5,1"
// See https://www.theiphonewiki.com/wiki/Models
// OS X:                        returns "x86_64"
//
var model = device.model;

Android 特性

iOS 特性

模型值基於 Apple 提供的識別碼。

如果您需要確切的裝置名稱,例如 iPhone 13 Pro Max,則需要建立對應器來將已知的識別碼轉換為關聯的裝置名稱。

範例:識別碼 iPhone14,3 與裝置 iPhone 13 Pro Max 相關聯。

如需所有識別碼對裝置名稱的完整清單,請參閱這裡

device.platform

取得裝置的作業系統名稱。

var string = device.platform;

支援平台

  • Android
  • 瀏覽器
  • iOS
  • Windows
  • OS X

快速範例

// Depending on the device, a few examples are:
//   - "Android"
//   - "browser"
//   - "iOS"
//   - "WinCE"
//   - "Mac OS X"
//
var devicePlatform = device.platform;

device.uuid

取得裝置的通用唯一識別碼 (UUID)。

var string = device.uuid;

說明

UUID 的產生方式細節由裝置製造商決定,並且特定於裝置的平台或型號。

支援平台

  • Android
  • iOS
  • Windows
  • OS X

快速範例

// Android: Returns a random 64-bit integer (as a string, again!)
//
// iOS: (Paraphrased from the UIDevice Class documentation)
//         Returns the [UIDevice identifierForVendor] UUID which is unique and the same for all apps installed by the same vendor. However the UUID can be different if the user deletes all apps from the vendor and then reinstalls it.
//
// Windows Phone 7 : Returns a hash of device+current user,
// if the user is not defined, a guid is generated and will persist until the app is uninstalled
//
var deviceID = device.uuid;

Android 特性

Android 上的 uuid 是一個 64 位元的整數(表示為十六進位字串)。此 uuid 的行為在兩個不同的作業系統版本上是不同的 -

對於 < Android 8.0 (API 等級 26)

在低於 Android 8.0 的平台版本中,uuid 是在使用者第一次設定裝置時隨機產生的,並且在使用者裝置的生命週期內應保持不變。

對於 Android 8.0 或更高版本

上述行為在 Android 8.0 中被更改。請在這裡詳細閱讀 這裡

在 Android 8.0 及更高版本中,uuid 對於每個應用程式簽署金鑰、使用者和裝置的組合將是唯一的。該值由簽署金鑰和使用者確定。如果在裝置上執行恢復原廠設定或 APK 簽署金鑰變更,則該值可能會變更。

請在這裡閱讀更多 https://developer.android.com/reference/android/provider/Settings.Secure#ANDROID_ID。

iOS 特性

iOS 上的 uuid 使用 identifierForVendor 屬性。它在相同供應商的裝置之間是唯一的,但對於不同的供應商將會有所不同,並且如果刪除該供應商的所有應用程式,然後重新安裝,則會變更。請參閱 這裡 以了解詳細資訊。如果應用程式是從備份或 iCloud 還原,則 UUID 將會相同,因為它會儲存在偏好設定中。使用此外掛程式舊版本的用戶仍將收到先前由其他方式產生的相同 UUID,因為它將從偏好設定中擷取。

OS X 特性

OS X 上的 uuid 會在尚不存在時自動產生,並儲存在 standardUserDefaultsCDVUUID 屬性中。

device.version

取得作業系統版本。

var string = device.version;

支援平台

  • Android
  • 瀏覽器
  • iOS
  • Windows
  • OS X

快速範例

// Android:    Froyo OS would return "2.2"
//             Eclair OS would return "2.1", "2.0.1", or "2.0"
//             Version can also return update level "2.1-update1"
//
// Browser:    Returns version number for the browser
//
// iOS:     iOS 3.2 returns "3.2"
//
// Windows 8: return the current OS version, ex on Windows 8.1 returns 6.3.9600.16384
//
// OS X:        El Capitan would return "10.11.2"
//
var deviceVersion = device.version;

device.manufacturer

取得裝置的製造商。

var string = device.manufacturer;

支援平台

  • Android
  • iOS
  • Windows

快速範例

// Android:    Motorola XT1032 would return "motorola"
// iOS:     returns "Apple"
//
var deviceManufacturer = device.manufacturer;

device.isVirtual

裝置是否在模擬器上執行。

var isSim = device.isVirtual;

device.sdkVersion (僅限 Android)

取得 Android 裝置的 SDK 版本 (SDK_INT)。

支援平台

  • Android

OS X 和瀏覽器特性

OS X 和瀏覽器上的 isVirtual 屬性始終傳回 false。

device.serial

取得裝置硬體序號 (SERIAL)。

var string = device.serial;

支援平台

  • Android
  • OS X

Android 特性

自 Android 9 起,為 uuid 屬性提供支援的底層原生 API 已被棄用,並且在沒有適當權限的情況下將始終傳回 UNKNOWN。Cordova 從未實作處理所需的權限。自 Android 10 起,所有不可重設的裝置識別碼都無法再由普通應用程式讀取,並且將始終傳回 UNKNOWN。 更多資訊可以 在這裡閱讀

device.isiOSAppOnMac

iOS 應用程式正在 Mac 桌面(Apple Silicon ARM64 處理器,M1 或更新版本)上執行。此參數僅適用於 iOS V14.0 或更高版本,並且不會針對 Android 裝置傳回。

var boolean = device.isiOSAppOnMac;

支援平台

  • iOS