Skip to main content

Platform

Platform サービスでは、現在のデバイスの情報を取得できます。platforms メソッドは、そのデバイスに該当するすべてのプラットフォームを返します。たとえば、タブレットで表示しているか、モバイル端末とブラウザーのどちらで実行しているか、OS は何か(iOS、Android など)を判別できます。Platform サービスでは、画面の向きや、言語の書字方向が右から左かどうかなども取得できます。これらの情報を使ってデバイスに合わせてアプリをカスタマイズできます。

Usage​

import { Platform } from '@ionic/angular/lazy';

@Component({...})
export class MyPage {
constructor(public platform: Platform) {

}
}

Methods​

is​

Descriptionユーザーが使用しているプラットフォームに応じて、is(platformName)は true または false を返します。ただし、同じアプリが複数のプラットフォーム名で true を返すこともあります。例えば、iPad で実行されているアプリは、mobile、ios、ipad、tabletといったプラットフォーム名で true を返します。また、もしアプリが Cordova を通じて実行されている場合は、cordovaも true になります。
Signatureis(platformName: Platforms) => boolean

Parameters​

NameTypeDescription
platformNamePlatformsプラットフォームの名前です。利用可能なオプションは、android、capacitor、cordova、desktop、electron、hybrid、ios、ipad、iphone、mobile、phablet、pwa、tablet です。

Platforms​

以下は、利用可能なすべての platform の値とそれに対応する説明をまとめた表です。

Platform NameDescription
androidAndroid を搭載したデバイスです。
capacitorCapacitor を実行しているデバイス
cordovaCordova を実行しているデバイス
desktopa desktop device
electronElectron を使っているデスクトップデバイスです。
hybridCapacitor または Cordova を実行しているデバイス
iosiOS を搭載したデバイスです。
ipadan iPad device
iphonean iPhone device
mobilea mobile device
mobilewebモバイルデバイス上で動作するウェブブラウザ
phableta phablet device
pwaa PWA app
tableta tablet device

プラットフォーム検出機能のカスタマイズ​

特定のプラットフォームを検出する関数は、グローバルな Ionic config に別の関数を指定して上書きできます。各関数は引数に window を取り、真偽値を返します。

app.module.ts
/*
* IonicModule is deprecated and will be removed in a future major version.
* Refer to the "Angular (Standalone)" tab to use `provideIonicAngular()` instead.
*/
import { IonicModule } from '@ionic/angular/lazy';

@NgModule({
...
imports: [
BrowserModule,
IonicModule.forRoot({
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;
}
},
}),
AppRoutingModule
],
...
})
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;
};

platforms​

Descriptionデバイスによっては platforms が複数の値を返します。これらの値はプラットフォームの階層を表します。たとえば iPhone では mobile、ios、iphone を返します。
Signatureplatforms() => string[]

ready​

Descriptionプラットフォームの準備が整い、ネイティブ機能を呼び出せるようになった時点で解決する Promise を返します。Web ブラウザーでは DOM の準備が整ったとき、Cordova などのアプリケーションエンジンでは Cordova が deviceready イベントを発行したときに解決します。解決値の readySource は使用中のプラットフォームを示します。

たとえば Cordova の準備が整ったときの値は cordova です。デフォルト値は dom です。readySource はプラットフォームによって処理を変えたい場合に便利です。たとえば、Status Bar プラグインは Capacitor と Cordova でのみ実行できるため、Web ではこのプラグインを呼び出す処理を実行しません。
Signatureready() => Promise<string>

isRTL​

Descriptionこのアプリが右から左への言語方向を使用しているかどうかを返します。おすすめは、アプリのindex.htmlファイルに、dir属性の値が正しく設定されていることです。例えば、<html dir="ltr">や<html dir="rtl">のようにです。W3C: Structural markup and right-to-left text in HTML
SignatureisRTL() => boolean

isLandscape​

Descriptionアプリが横向きのモードである場合、trueを返します。
SignatureisLandscape() => boolean

isPortrait​

Descriptionアプリが縦向きモードにある場合は、trueを返します。
SignatureisPortrait() => boolean

width​

Descriptionwindow.innerWidthを使って、プラットフォームの viewport の幅を取得します。
Signaturewidth() => number

height​

Descriptionwindow.innerHeightを使用して、プラットフォームの viewport の高さを取得します。
Signatureheight() => number

url​

Description現在の URL を取得します。
Signatureurl() => string

testUserAgent​

Descriptionもし、その式がユーザーエージェントの文字列に含まれている場合、trueを返します。
SignaturetestUserAgent(expression: string) => boolean

Parameters​

NameTypeDescription
expressionstringユーザーエージェント内でチェックする文字列

Events​

pause​

pause イベントは、ネイティブ・プラットフォームがアプリケーションをバックグラウンドに置いたとき、通常はユーザーが別のアプリケーションに切り替えたときに発生します。このイベントは、Cordova/Capacitor アプリケーションがバックグラウンドに置かれたときに発生しますが、標準的な Web ブラウザでは発生しません。

Examples​

this.platform.pause.subscribe(async () => {
alert('Pause event detected');
});

resize​

resize イベントは、ブラウザウィンドウの寸法が変更されたときに発生します。これは、ブラウザーウィンドウが物理的にサイズ変更されている場合や、デバイスの向きが変わっている場合に発生します。

Examples​

this.platform.resize.subscribe(async () => {
alert('Resize event detected');
});

resume​

resume イベントは、ネイティブプラットフォームがバックグラウンドからアプリケーションを引き出したときに発生します。このイベントは、Cordova/Capacitor アプリがバックグラウンドから出てきても、標準的な Web ブラウザで起動しない場合に発生します。

Examples​

this.platform.resume.subscribe(async () => {
alert('Resume event detected');
});