Thursday 18 October 2018

detect webview or browser


This is working and tested in all platforms

Step1: create a Utility file

export class Utility {
constructor() { }

public static get isMobile(): boolean {

if (this.isIOS || this.isAndroid) {
return true;
} else {
return false;
}
}

public static get isWebview(): boolean {
const useragent = navigator.userAgent;
const rules = ['WebView', '(iPhone|iPod|iPad)(?!.*Safari\/)', 'Android.*(wv|\.0\.0\.0)'];
const regex = new RegExp(`(${rules.join('|')})`, 'ig');
return Boolean(useragent.match(regex));
}

public static get isWindowsPhone(): boolean {
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 true;
} else {
return false;
}
}

public static get isAndroid(): boolean {
const userAgent = navigator.userAgent || navigator.vendor || window['opera'];
return /android/i.test(userAgent);
}

public static get isIOS(): boolean {
const userAgent = navigator.userAgent || navigator.vendor || window['opera'];
if (/iPad|iPhone|iPod/.test(userAgent) && !window['MSStream']) {
return true;
} else {
return false;
}
}
}

Step2: import in ur file .ts
import { Utility } from '../../classes/util';

Usage :
if (Utility.isWebview && Utility.isAndroid) {

} else if (Utility.isWebview && Utility.isIOS) {

}

1 comment:

Do you think it could be useful for you? Share your thoughts with us!