Platform
The Platform service can be used to get information about your current device. You can get all of the platforms associated with the device using the platforms
method, including whether the app is being viewed from a tablet, if it's on a mobile device or browser, and the exact platform (iOS, Android, etc). You can also get the orientation of the device, if it uses right-to-left language direction, and much much more. With this information you can completely customize your app to fit any device.
Usage
import { Platform } from '@ionic/angular';
@Component({...})
export class MyPage {
constructor(public platform: Platform) {
}
}
Methods
is
Description | Depending on the platform the user is on, is(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. |
Signature | is(platformName: Platforms) => boolean |
Parameters
Name | Type | Description |
---|---|---|
platformName | Platforms | Name of the platform. Available options are android, capacitor, cordova, desktop, electron, hybrid, ios, ipad, iphone, mobile, phablet, pwa, tablet |
Platforms
Below is a table listing all the possible platform values along with corresponding descriptions.
Platform Name | Description |
---|---|
android | a device running Android |
capacitor | a device running Capacitor |
cordova | a device running Cordova |
desktop | a desktop device |
electron | a desktop device running Electron |
hybrid | a device running Capacitor or Cordova |
ios | a device running iOS |
ipad | an iPad device |
iphone | an iPhone device |
mobile | a mobile device |
mobileweb | a web browser running in a mobile device |
phablet | a phablet device |
pwa | a PWA app |
tablet | a tablet device |
Customizing Platform Detection Functions
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.
import { IonicModule } from '@ionic/angular';
@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;
};