Platform
Platform サービスでは、現在のデバイスの情報を取得できます。platforms メソッドは、そのデバイスに該当するすべてのプラットフォームを返します。たとえば、タブレットで表示しているか、モバイル端末とブラウザーのどちらで実行しているか、OS は何か(iOS、Android など)を判別できます。Platform サービスでは、画面の向きや、言語の書字方向が右から左かどうかなども取得できます。これらの情報を使ってデバイスに合わせてアプリをカスタマイズできます。
Usage
- Angular
- Angular (Standalone)
import { Platform } from '@ionic/angular/lazy';
@Component({...})
export class MyPage {
constructor(public platform: Platform) {
}
}
import { Platform } from '@ionic/angular';
@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 になります。 |
| Signature | is(platformName: Platforms) => boolean |
Parameters
| Name | Type | Description |
|---|---|---|
platformName | Platforms | プラットフォームの名前です。利用可能なオプションは、android、capacitor、cordova、desktop、electron、hybrid、ios、ipad、iphone、mobile、phablet、pwa、tablet です。 |
Platforms
以下は、利用可能なすべての platform の値とそれに対応する説明をまとめた表です。
| 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 を取り、真偽値を返します。
- Angular
- Angular (Standalone)
/*
* 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
],
...
})
import { provideIonicAngular } from '@ionic/angular';
bootstrapApplication(AppComponent, {
providers: [
...,
provideIonicAngular({
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;
};
platforms
| Description | デバイスによっては platforms が複数の値を返します。これらの値はプラットフォームの階層を表します。たとえば iPhone では mobile、ios、iphone を返します。 |
| Signature | platforms() => string[] |
ready
| Description | プラットフォームの準備が整い、ネイティブ機能を呼び出せるようになった時点で解決する Promise を返します。Web ブラウザーでは DOM の準備が整ったとき、Cordova などのアプリケーションエンジンでは Cordova が deviceready イベントを発行したときに解決します。解決値の readySource は使用中のプラットフォームを示します。たとえば Cordova の準備が整ったときの値は cordova です。デフォルト値は dom です。readySource はプラットフォームによって処理を変えたい場合に便利です。たとえば、Status Bar プラグインは Capacitor と Cordova でのみ実行できるため、Web ではこのプラグインを呼び出す処理を実行しません。 |
| Signature | ready() => Promise<string> |
isRTL
| Description | このアプリが右から左への言語方向を使用しているかどうかを返します。おすすめは、アプリのindex.htmlファイルに、dir属性の値が正しく設定されていることです。例えば、<html dir="ltr">や<html dir="rtl">のようにです。W3C: Structural markup and right-to-left text in HTML |
| Signature | isRTL() => boolean |
isLandscape
| Description | アプリが横向きのモードである場合、trueを返します。 |
| Signature | isLandscape() => boolean |
isPortrait
| Description | アプリが縦向きモードにある場合は、trueを返します。 |
| Signature | isPortrait() => boolean |
width
| Description | window.innerWidthを使って、プラットフォームの viewport の幅を取得します。 |
| Signature | width() => number |
height
| Description | window.innerHeightを使用して、プラットフォームの viewport の高さを取得します。 |
| Signature | height() => number |
url
| Description | 現在の URL を取得します。 |
| Signature | url() => string |
testUserAgent
| Description | もし、その式がユーザーエージェントの文字列に含まれている場合、trueを返します。 |
| Signature | testUserAgent(expression: string) => boolean |
Parameters
| Name | Type | Description |
|---|---|---|
| expression | string | ユーザーエージェント内でチェックする文字列 |
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');
});