How to get platform information using Ionic 4.
In Ionic 3 , we can do as mentioned below.
if(this.platform.is('android') || this.platform.is('ios') || this.platform.is('core'))
{
}
But this is not working in Ionic 4. Can anyone please help me how to do this in Ionic 4 or what can be the alternate for this?
1 Like
Depending on what you want to do:
@Sujan12 How to detect desktop browser platform using this because when I tried with below it is not falling in ācoreā condition.
if (this.platform.is('core')) {
alert('core platform');
} else {
alert('something else');
}
No idea, the link I posted only refers to a is('cordova')
.
Is there a workaround for this yet?
Detecting ācoreā or desktop browser in any way other than screen size?
This just got updated yesterday. Looks like ācoreā is supposed to work. I know it doesnāt now, but maybe in the next update?
# 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.
<!-- Auto Generated Below -->
## Methods
| Method | Description |
|---------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `is` | Returns true/false based on platform. Available options are android, cordova, core, ios, ipad, iphone, mobile, mobileweb, phablet, tablet, windows, electron. |
| `platforms` | Returns an array of platforms that the current device matches. |
| `versions` | Returns an object that contains the major, minor, and patcher version of the current device. |
| `ready` | Returns a promise that resolves when the native bridge is ready in Cordova, or when the DOM is fully loaded in the browser. |
| `isLandscape` | Returns true of false if the device is in landscape. |
| `isPortrait` | Returns true of false if the device is in portrait. |
This file has been truncated. show original
1 Like
I am using
this.isApp = (!document.URL.startsWith(āhttpā) || document.URL.startsWith(āhttp://localhost:8080 ā));
So if isApp is true I am running native on Android or IOS else it is in the browser
@btsiders Great, looking forward to it. Thanks for the heads up.
@developergeme Thanks for that too. Iāll try it tomorrow if there is no update.
Cheers
Desktop is now detected in 4.0.0-beta.4
1 Like
For me itās still showing android platform while detecting in desktop .
Below is my ionic info :
Ionic:
ionic (Ionic CLI) : 4.1.1 (C:\Users\test\AppData\Roaming\npm\node_modules\ionic)
Ionic Framework : @ionic /angular 4.0.0-beta.1
@angular-devkit /core : 0.7.2
@angular-devkit /schematics : 0.7.2
@angular /cli : 6.1.2
@ionic /ng-toolkit : 1.0.5
@ionic /schematics-angular : 1.0.4
Cordova:
cordova (Cordova CLI) : 8.0.0
Cordova Platforms : android 6.2.3
Cordova Plugins : cordova-plugin-ionic-keyboard 2.0.5, cordova-plugin-ionic-webview 2.0.0, (and 4 other plugins)
System:
Android SDK Tools : 26.1.1 (C:\Users\test\AppData\Local\Android\sdk)
NodeJS : v8.11.3 (C:\Program Files\nodejs\node.exe)
npm : 5.6.0
OS : Windows 10
Your are still using beta.1
.
Here is a post with some upgrade instructions;
First way, with cmd, like:
npm rm @ionic/angular --save
npm install @ionic/angular --save
Second way, the āclean sheetā way:
edit manually package.json and change the version according what you want, like changing 4.0.0-beta.3 to 4.0.0-beta.5
then
rm -r node_modules
rm package-lock.json
npm install
conz: it wipe all libs and will recreate a new lock file, so in the meantime you could receive other dependencies update
pro: it makes every dependencies clean again
I use this method theā¦
Note: the current is Beta 5 but the desktop detection came in beta 4.
Thanks for upgrade instructions.
On Ionic 4 Platform is unber ā@ionic /angularā
import { Platform } from ā@ionic /angularā;
I did
console.log(platform.platforms());
to get the platform names when I was debugging on my browser. Note that this gives different platform names if you are testing from real android or ios device.
I also did
if(platform.is('mobileweb')){ .... }
to check if my platform was a browser.
i get the result same
if (this.platform.is(ādesktopā)) {
alert(ādesktopā);
} else if (this.platform.is(āiosā)) {
alert(āiosā);
} else if (this.platform.is(āandroidā)) {
alert(āandroidā);
// fallback to browser APIs
}
But default http://localhost:8100/home
show the result āandroid ā
I tried console.log(platform.platforms()); and tested this statement in different devices, once on browser and once on real device, it did return different platform values available.
i also use second way
But output get on browser
Thatās the thing. It outputs different platforms based on the device. So we canāt predict accurately which platform we are actually on.