Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {Injectable } from '@angular/core';
  2.  
  3. @Injectable()
  4. export class PlatformDetectorService
  5. {
  6.     private _userAgent: string;
  7.  
  8.     constructor() {
  9.         this._userAgent = window.navigator.userAgent.toLowerCase();
  10.     }    
  11.  
  12.     public getPlatform(): string
  13.     {
  14.         if (this.isTablet())
  15.         {
  16.             return "Tablet";
  17.         }
  18.         else if(this.isMobile())
  19.         {
  20.             return "Mobile";
  21.         }
  22.         else
  23.         {
  24.             return "Desktop";
  25.         }
  26.     }
  27.  
  28.     // Public properties
  29.  
  30.     get UserAgent(): string
  31.     {
  32.         return this._userAgent;
  33.     }
  34.  
  35.     private isTablet(): boolean
  36.     {
  37.         if(this._userAgent.match(/Tablet|iPad/i))
  38.         {
  39.             return true;
  40.         }
  41.         else
  42.         {
  43.             return false;
  44.         }
  45.     }
  46.  
  47.     private isMobile(): boolean
  48.     {
  49.         if(this._userAgent.match(/Mobile|Windows Phone|Lumia|Android|webOS|iPhone|iPod|Blackberry|PlayBook|BB10|Opera Mini|\bCrMo\/|Opera Mobi/i))
  50.         {
  51.             return true;
  52.         }
  53.         else
  54.         {
  55.             return false;
  56.         }
  57.     };
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement