Yevhen Klymentiev
dark
light
console
darkness
y.klymentiev@gmail.com
Reusable Snippets|Practical utility code for everyday use — custom-built and ready to share

isWebView

Determines if the current environment is a WebView. Checks for common patterns in user agent and navigator properties.

TypeScript
Copied!
1/**
2 * Determines if the current environment is a WebView.
3 * Checks for common patterns in user agent and navigator properties.
4 *
5 * @returns True if inside a WebView, false otherwise.
6 */
7export function isWebView(): boolean {
8  if (typeof navigator === 'undefined' || typeof window === 'undefined') return false;
9
10  const ua = navigator.userAgent || '';
11  const standalone = (window.navigator as any).standalone;
12
13  const isIOSWebView = /iPhone|iPod|iPad/.test(ua) && !/Safari/.test(ua);
14  const isAndroidWebView =
15    /Android/.test(ua) && /Version\/[\d.]+/.test(ua) && !/Chrome/.test(ua);
16
17  return Boolean(isIOSWebView || isAndroidWebView || standalone);
18}
  • Platform-Specific Heuristics

    Separately handles iOS and Android by using distinct, well-known WebView indicators in the user agent string.

  • Fallback Support for iOS Standalone Mode

    Accounts for iOS's navigator.standalone flag, which is often used by PWAs and WebViews.

  • No External Dependencies

    Pure JavaScript logic with no reliance on third-party libraries, making it lightweight and portable.

  • Safe Execution in Non-Browser Environments

    Guards against runtime errors with early checks for navigator and window presence.

Tests | Examples

TypeScript
Copied!
1const originalNavigator = global.navigator;
2const originalWindow = global.window;
3
4afterEach(() => {
5  global.navigator = originalNavigator;
6  global.window = originalWindow;
7});
8
9test('detects iOS WebView (no Safari in UA)', () => {
10  // @ts-ignore
11  global.navigator = {
12    userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_0) AppleWebKit/605.1.15'
13  };
14  // @ts-ignore
15  global.window = { navigator: { standalone: false } };
16  expect(isWebView()).toBe(true);
17});
18
19test('detects Android WebView (Version present, no Chrome)', () => {
20  // @ts-ignore
21  global.navigator = {
22    userAgent: 'Mozilla/5.0 (Linux; Android 9) AppleWebKit/537.36 Version/4.0'
23  };
24  // @ts-ignore
25  global.window = { navigator: {} };
26  expect(isWebView()).toBe(true);
27});
28
29test('returns false for standard browser (Chrome)', () => {
30  // @ts-ignore
31  global.navigator = {
32    userAgent: 'Mozilla/5.0 (Windows NT 10.0) Chrome/90.0.4430.93'
33  };
34  // @ts-ignore
35  global.window = { navigator: {} };
36  expect(isWebView()).toBe(false);
37});
38
39test('returns false if navigator or window are undefined', () => {
40  // @ts-ignore
41  delete global.navigator;
42  // @ts-ignore
43  delete global.window;
44  expect(isWebView()).toBe(false);
45});

Common Use Cases

  • Custom Behavior Inside Mobile Apps

    Adapt functionality (e.g., in-app navigation, API calls) when running in embedded WebViews.

  • Analytics Differentiation

    Distinguish WebView usage from standard browsers for more accurate usage tracking.

  • UI Adjustments for Embedded Views

    Suppress browser-specific UI elements or show app-specific controls when inside a WebView.

  • Prevent External Navigation

    Avoid deep linking or external redirects from within a WebView to preserve app context.

Codebase: Utilities -> Platforms -> isWebView | Yevhen Klymentiev