@napacitor/device
设备基础信息插件(与 @capacitor/device 对应)。
安装方式
To use npm:
npm install @napacitor/device
To use yarn:
yarn add @napacitor/device
Sync native files:
npx nap sync
用法
import { Device } from '@napacitor/device';
const info = await Device.getInfo();
// { model, platform, osVersion, manufacturer, isVirtual, name? }
const lang = await Device.getLanguageCode(); // { value: 'zh' }
const locale = await Device.getLocale(); // { value: 'zh-CN' }
实现原理
封装 .NET MAUI 的 DeviceInfo 文档所述 API:DeviceInfo.Current(Model / Manufacturer / Name / VersionString / DeviceType)与 CultureInfo(语言 / 区域)。isVirtual = DeviceType == Virtual;platform 取 Napacitor 运行时平台名(android / ios / windows / mac)。
权限
Android
无需额外配置。
iOS
无需额外配置。
Windows
无需额外配置。
API
getInfo()
获取设备信息。
返回值: Promise<DeviceInfo>
getLanguageCode()
获取当前 UI 语言代码(CultureInfo.CurrentUICulture.TwoLetterISOLanguageName)。
返回值: Promise<GetLanguageCodeResult>
getLocale()
获取当前区域设置(CultureInfo.CurrentCulture.Name)。
返回值: Promise<GetLocaleResult>
Interfaces
DeviceInfo
| 属性 | 类型 | 描述 |
|---|---|---|
model | string | 设备型号(如 iPhone13,2)。 |
platform | string | 平台:android / ios / mac / windows / web。 |
osVersion | string | 操作系统版本。 |
manufacturer | string | 制造商。 |
isVirtual | boolean | 是否为模拟器/虚拟设备(DeviceType == Virtual)。 |
name | string | 设备名称(部分平台提供,可能为空)。 |
GetLanguageCodeResult
| 属性 | 类型 | 描述 |
|---|---|---|
value | string | 当前 UI 语言代码(如 zh、en)。 |
GetLocaleResult
| 属性 | 类型 | 描述 |
|---|---|---|
value | string | 当前区域设置(如 zh-CN)。 |