Created
February 28, 2018 07:03
-
-
Save rainstormza/6372890bb8840349f2f6ed7123291539 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit } from '@angular/core'; | |
declare const iOS: any; | |
declare const Android: any; | |
@Component({ | |
selector: 'app-root', | |
templateUrl: './app.component.html', | |
styleUrls: ['./app.component.scss'] | |
}) | |
export class AppComponent implements OnInit { | |
constructor() { } | |
ngOnInit() { | |
this.callMobileFunction(); | |
} | |
getMobileOperatingSystem() { | |
const userAgent = navigator.userAgent || navigator.vendor || window['opera']; | |
// Windows Phone must come first because its UA also contains "Android" | |
if (/windows phone/i.test(userAgent)) { | |
return 'Windows Phone'; | |
} | |
if (/android/i.test(userAgent)) { | |
return 'Android'; | |
} | |
// iOS detection from: http://stackoverflow.com/a/9039885/177710 | |
if (/iPad|iPhone|iPod/.test(userAgent) && !window['MSStream']) { | |
return 'iOS'; | |
} | |
return 'unknown'; | |
} | |
callMobileFunction() { | |
const checkDevice = this.getMobileOperatingSystem(); | |
if (checkDevice === 'iOS') { | |
iOS.calliOSFunction(); // function in ios | |
} else if (checkDevice === 'Android') { | |
Android.callAndroidFunction(); // function in android | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment