跳到主要内容

@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​

属性类型描述
modelstring设备型号(如 iPhone13,2)。
platformstring平台:android / ios / mac / windows / web。
osVersionstring操作系统版本。
manufacturerstring制造商。
isVirtualboolean是否为模拟器/虚拟设备(DeviceType == Virtual)。
namestring设备名称(部分平台提供,可能为空)。

GetLanguageCodeResult​

属性类型描述
valuestring当前 UI 语言代码(如 zh、en)。

GetLocaleResult​

属性类型描述
valuestring当前区域设置(如 zh-CN)。