How to find device os and version in ionic

旧街凉风 提交于 2019-12-11 05:08:08

问题


I have worked on few tutorials and at last found something regarding this in ionic framework.....

They gave some codes that use ionic native and i did the same by embedding the below code:

//Display OS name and version (Start)
import { Device } from 'ionic-native';


console.log('Device OS is: ' + Device.device.platform);
console.log('Device OS Version is: ' + Device.device.version);

//Display OS name and version (End) 

While using ionic serve i got this in the console:

'Device OS is: undefined'
'Device OS version is: undefined'

Then i guessed it wont work in browser and i tried building and run in my phone but still the same log comes...

PS: I have just started with ionic

Thank you in advance :)


回答1:


Please see this link http://ionicframework.com/docs/api/utility/ionic.Platform/

angular.module('PlatformApp', ['ionic'])
.controller('PlatformCtrl', function($scope) {

  ionic.Platform.ready(function(){
    // will execute when device is ready, or immediately if the device is already ready.
  });

  var deviceInformation = ionic.Platform.device();

  var isWebView = ionic.Platform.isWebView();
  var isIPad = ionic.Platform.isIPad();
  var isIOS = ionic.Platform.isIOS();
  var isAndroid = ionic.Platform.isAndroid();
  var isWindowsPhone = ionic.Platform.isWindowsPhone();

  var currentPlatform = ionic.Platform.platform();
  var currentPlatformVersion = ionic.Platform.version();

  ionic.Platform.exitApp(); // stops the app
});



回答2:


I used cordova-plugin-device to fetch uuid number and device model name. It fetches all possible information about the device. I believe, It will also serve your purpose. Inside onDeviceReady() function, you just assign the values to your convenient variables, like: var deviceOS = device.uuid; var deviceVersion = device.version; Hope It helps. Please let me know if it works. :)




回答3:


You can get the device os and version in ionic

import { Device } from '@ionic-native/device';
import { Platform } from 'ionic-angular';

constructor(private device: Device,public platform: Platform) { 

   console.log('Device UUID is: ' + this.device.uuid);

   if (this.platform.is('ios')) {
    // This will only print when on iOS
     console.log('I am an iOS device!');
   }
   if (this.platform.is('android')) {
    // This will only print when on Android
     console.log('I am an android device!');
   }
}



回答4:


In Ionic 3 you can use device this way,

import { Device } from '@ionic-native/device';

private platformType:any;
private versionType:any;

constructor(private device: Device) { 
this.platformType = this.device.platform;
this.versionType = this.device.version;
}

Now this can be used in the html file using data-binding ex:

<ion-grid>
  <ion-row>
   <ion-col>
     <h1>Platform Type: {{platformType}}</h1>
   </ion-col>
  </ion-row>
  <ion-row>
   <ion-col>
    <h1>OS Version: {{versionType}}</h1>
   </ion-col>
  </ion-row>
</ion-grid>

Note: In the browser the variables will log null. The Device plugin function will only work on the native devices. You can test your device with

ionic cordova run (android or ios) --device


来源:https://stackoverflow.com/questions/38967059/how-to-find-device-os-and-version-in-ionic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!