跳至主要內容
版本:v8

平台

isPlatform

isPlatform 方法可用於測試您的應用程式是否在特定平台上執行

import { isPlatform } from '@ionic/react';

isPlatform('ios'); // returns true when running on a iOS device

根據使用者所在的平台,isPlatform(platformName) 將會傳回 true 或 false。請注意,同一個應用程式可能會傳回多個平台名稱的 true。例如,從 iPad 執行的應用程式會傳回平台名稱:mobile、ios、ipad 和 tablet 的 true。此外,如果應用程式是從 Cordova 執行,則 cordova 也會是 true。

getPlatforms

getPlatforms 方法可用於判斷您的應用程式目前在哪些平台上執行。

import { getPlatforms } from '@ionic/react';

getPlatforms(); // returns ["iphone", "ios", "mobile", "mobileweb"] from an iPhone

根據您使用的裝置,getPlatforms 可以傳回多個值。每個可能的值都是平台的階層結構。例如,在 iPhone 上,它會傳回 mobile、ios 和 iphone。

平台

以下表格列出所有可能的平台值以及對應的描述。

平台名稱描述
android執行 Android 的裝置
capacitor執行 Capacitor 的裝置
cordova執行 Cordova 的裝置
desktop桌上型裝置
electron執行 Electron 的桌上型裝置
hybrid執行 Capacitor 或 Cordova 的裝置
ios執行 iOS 的裝置
ipadiPad 裝置
iphoneiPhone 裝置
mobile行動裝置
mobileweb在行動裝置中執行的網頁瀏覽器
phablet平板手機裝置
pwaPWA 應用程式
tablet平板裝置

自訂平台偵測函式

用於偵測特定平台的函式可以透過在全域Ionic 設定中提供替代函式來覆寫。每個函式都會將 window 作為參數並傳回布林值。

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;
};