Platform
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 が返されます。
Platforms
次の表に、使用可能なすべてのプラットフォーム値とその説明を示します。
| Platform Name | Description |
|---|---|
| android | Android を搭載したデバイスです。 |
| capacitor | Capacitor を実行しているデバイス |
| cordova | Cordova を実行しているデバイス |
| desktop | a desktop device |
| electron | Electron を使っているデスクトップデバイスです。 |
| hybrid | Capacitor または Cordova を実行しているデバイス |
| ios | iOS を搭載したデバイスです。 |
| ipad | an iPad device |
| iphone | an iPhone device |
| mobile | a mobile device |
| mobileweb | モバイルデバイス上で動作するウェブブラウザ |
| phablet | a phablet device |
| pwa | a PWA app |
| tablet | a tablet device |
プラットフォーム検出機能のカスタマイズ
特定のプラットフォームを検出する関数は、グローバルな Ionic config に別の関数を指定して上書きできます。各関数は引数に 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;
};