简体   繁体   English

如何检测移动设备

[英]How to detect mobile device

I am building a mobile site using the ionic framework.我正在使用离子框架构建一个移动站点。 I want to detect mobile devices(Android and iOS) with AngularJS (or ionic).我想用 AngularJS(或 ionic)检测移动设备(Android 和 iOS)。

If access device is Android → #/android if access device is iOS → #/ios如果接入设备是Android → #/android 如果接入设备是iOS → #/ios

controllers.js控制器.js

function uaCtrl('$scope', '$location', ($scope, $location) {
$scope = function () {
  var isMobile = {
    Android: function() {
      return navigator.userAgent.match(/Android/i);
    },
    iOS: function() {
      return navigator.userAgent.match(/iPhone | iPad | iPod/i)
    }
  }

  if(isMobile.Android()) {
    $location.path('#/android');
  }else if(isMobile.iOS()) {
    $location.path('#/ios');
  }else{
    $location.path('#/ios');
  }

};

}; };

app.js应用程序.js

.config(function($stateProvider, $urlRouterProvider) {
  $urlRouterProvider.otherwise('/dash');

  $stateProvider

  .state('home', {
      url: '/dash',
      templateUrl: 'templates/dash.html'
    })

  .state('android-home', {
    url: '/android',
    templateUrl:'templates/dash-android.html'
  })

  .state('ios-home', {
    url: '/ios',
    templateUrl:'templates/dash-ios.html'
  })
});

dash.html破折号.html

<ion-view hide-nav-bar="true" ng-controller="uaCtrl">
    ?????
</ion-view>

ionic.Platform.isIOS() I believe is what you're looking for. ionic.Platform.isIOS()我相信这就是你要找的。

Platform docs: http://ionicframework.com/docs/api/utility/ionic.Platform/平台文档: http : //ionicframework.com/docs/api/utility/ionic.Platform/

It's unit tests: https://github.com/driftyco/ionic/blob/master/test/unit/angular/service/platform.unit.js这是单元测试: https : //github.com/driftyco/ionic/blob/master/test/unit/angular/service/platform.unit.js

However, I wouldn't recommend using different markup structures if you don't have to.但是,如果您不需要,我不建议您使用不同的标记结构。 Instead, if you have different styles depending on the platform, Ionic places platform-android or platform-ios in the body tag class, so you could do things like:相反,如果您根据平台有不同的样式,Ionic 会将platform-androidplatform-ios放在 body 标签类中,因此您可以执行以下操作:

.platform-android h1 { color: green; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM