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.
import { Platform } from '@ionic/angular';
@Component({...})
export class MyPage {
constructor(public platform: Platform) {
}
}
import { Platform } from '@ionic/angular/standalone';
@Component({...})
export class MyPage {
constructor(public platform: Platform) {
}
}
isis(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.is(platformName: Platforms) => booleanplatformNamePlatformsBelow is a table listing all the possible platform values along with corresponding descriptions.
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;
};
platformsplatforms can return multiple values. Each possible value is a hierarchy of platforms. For example, on an iPhone, it would return mobile, ios, and iphone.platforms() => string[]readydeviceready event. The resolved value is the readySource, which states the platform that was used. cordova. The default ready source value will be dom. The readySource is useful if different logic should run depending on the platform the app is running from. For example, only Capacitor and Cordova can execute the status bar plugin, so the web should not run status bar plugin logic.ready() => Promise<string>isRTLindex.html file already has the correct dir attribute value set, such as <html dir="ltr"> or <html dir="rtl">. W3C: Structural markup and right-to-left text in HTMLisRTL() => booleanisLandscapetrue if the app is in landscape mode.isLandscape() => booleanisPortraittrue if the app is in portrait mode.isPortrait() => booleanwidthwindow.innerWidth.width() => numberheightwindow.innerHeight.height() => numberurlurl() => stringtestUserAgenttrue if the expression is included in the user agent string.testUserAgent(expression: string) => booleanpauseThe pause event emits when the native platform puts the application into the background, typically when the user switches to a different application. This event emits when a Cordova/Capacitor app is put into the background but doesn't fire in a standard web browser.
this.platform.pause.subscribe(async () => {
alert('Pause event detected');
});
resizeThe resize event emits when the browser window has changed dimensions. This could be from a browser window being physically resized, or from a device changing orientation.
this.platform.resize.subscribe(async () => {
alert('Resize event detected');
});
resumeThe resume event fires when the native platform pulls the application out from the background. This event emits when a Cordova/Capacitor app comes out from the background but doesn't fire in a standard web browser.
this.platform.resume.subscribe(async () => {
alert('Resume event detected');
});