平台
¥Platform
isPlatform
isPlatform
方法可用于测试你的应用是否在某个平台上运行:
¥The isPlatform
method can be used to test if your app is running on a certain platform:
import { isPlatform } from '@ionic/react';
isPlatform('ios'); // returns true when running on a iOS device
根据用户所在的平台,isPlatform(platformName) 将返回 true 或 false。请注意,同一应用可以针对多个平台名称返回 true。例如,从 iPad 运行的应用将为平台名称返回 true:手机、ios、ipad 和平板电脑。此外,如果应用从 Cordova 运行,则 cordova 为 true。
¥Depending on the platform the user is on, isPlatform(platformName) will return true or false. Note that the same app can return true for more than one platform name. For example, an app running from an iPad would return true for the platform names: mobile, ios, ipad, and tablet. Additionally, if the app was running from Cordova then cordova would be true.
getPlatforms
getPlatforms
方法可用于确定你的应用当前在哪些平台上运行。
¥The getPlatforms
method can be used to determine which platforms your app is currently running on.
import { getPlatforms } from '@ionic/react';
getPlatforms(); // returns ["iphone", "ios", "mobile", "mobileweb"] from an iPhone
根据你使用的设备,getPlatforms
可以返回多个值。每个可能的值都是平台的层次结构。例如,在 iPhone 上,它将返回 mobile、ios 和 iphone。
¥Depending on what device you are on, getPlatforms
can return multiple values. Each possible value is a hierarchy of platforms. For example, on an iPhone, it would return mobile, ios, and iphone.
平台
¥Platforms
下表列出了所有可能的平台值以及相应的描述。
¥Below is a table listing all the possible platform values along with corresponding descriptions.
平台名称 | 描述 |
---|---|
android | 运行 Android 的设备 |
capacitor | 运行 Capacitor 的设备 |
cordova | 运行 Cordova 的设备 |
desktop | 桌面设备 |
electron | 运行 Electron 的桌面设备 |
hybrid | 运行 Capacitor 或 Cordova 的设备 |
ios | 运行 iOS 的设备 |
ipad | iPad 设备 |
iphone | iPhone 设备 |
mobile | 移动设备 |
mobileweb | 在移动设备上运行的网络浏览器 |
phablet | 平板手机设备 |
pwa | 一个 PWA 应用 |
tablet | 平板电脑设备 |
自定义平台检测功能
¥Customizing Platform Detection Functions
用于检测特定平台的函数可以通过在全局 Ionic 配置.h 中提供替代函数来覆盖。每个函数都以 window
作为参数并返回一个布尔值。
¥The function used to detect a specific platform can be overridden by providing an alternative function in the global Ionic config. Each function takes window
as a parameter and returns a boolean.
setupIonicReact({
platform: {
/** The default `desktop` function returns false for devices with a touchscreen.
* This is not always wanted, so this function tests the User Agent instead.
**/
desktop: (win) => {
const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(win.navigator.userAgent);
return !isMobile;
},
},
});
type PlatformConfig = {
android?: ((win: Window) => boolean) | undefined;
capacitor?: ((win: Window) => boolean) | undefined;
cordova?: ((win: Window) => boolean) | undefined;
desktop?: ((win: Window) => boolean) | undefined;
electron?: ((win: Window) => boolean) | undefined;
hybrid?: ((win: Window) => boolean) | undefined;
ios?: ((win: Window) => boolean) | undefined;
ipad?: ((win: Window) => boolean) | undefined;
iphone?: ((win: Window) => boolean) | undefined;
mobile?: ((win: Window) => boolean) | undefined;
mobileweb?: ((win: Window) => boolean) | undefined;
phablet?: ((win: Window) => boolean) | undefined;
pwa?: ((win: Window) => boolean) | undefined;
tablet?: ((win: Window) => boolean) | undefined;
};