如何判断我是否在使用Ionic2的移动平台上?

时间:2016-04-24 18:11:53

标签: javascript android ios angular ionic2

我开始学习ionic2来加速我的开发过程。 我的主要关注点是网络,移动是一个奖励。我设法调整了ionic2 gulpfile,以便从golang后端提供服务。这一切都很好,但我需要一种方法来确定我是在Android还是ios上,以便更改我的api发送请求的域位置.Eg:

开发我的应用程序时在localhost:8080这样做 document.location.hostname将返回localhost,我可以向localhost发出请求:8080 / api / endpoint

在制作中我的应用程序在www.wonderfulapp.com上提供服务 document.location.hostname将返回www.wonderfulapp.com,我可以向www.wonderfulapp.com / api / endpoint

发出请求

我希望我的代码确保在从android和ios发出请求时使用www.wonderfulapp.com。我该怎么做?

1 个答案:

答案 0 :(得分:1)

You need to inject the Platform class as described below:

import {Platform} from 'ionic-angular';

@Page({...})
export class MyPage {
  constructor(platform: Platform) {
  }
}

or like this with ES6:

@Page({...})
export class MyPage {
  constructor(platform) {
  }

  static get parameters() {
    return [[Platform]];
  }
}

The Platform class has a is method to check the target platform. If you want to check that a particular device, you can use the following values as parameter of this method:

  • mobileweb in a browser on a mobile device.
  • mobile on a mobile device.
  • android on a device running Android.
  • ios on a device running iOS.

Here is a sample:

@Page({...})
export class MyPage {
  constructor(platform: Platform) {
    if (platform.is('mobileweb')) {
      // Do something
    }
  }
}

See this doc for more details:

相关问题