Detect android webview

前端 未结 3 1453
别那么骄傲
别那么骄傲 2021-01-21 01:45

I have an html-javascript page, and I need to detect whenever it open on web view (Like inside facebook webview, twitter webview, etc.), and if it a webview - displ

相关标签:
3条回答
  • 2021-01-21 02:27

    You can't detect it by only using the user agent string, as any app that uses WebView can set the UA string to anything it wants.

    If you still insist on using the UA string, this is the explanation of the official docs: initially, Chromium-based WebView was using .0.0.0 as Chrome version suffix; in the newer versions ; wv was added into the platform part of the UA string. Note that pre-KitKat WebViews didn't have the Chrome part. See older posts about that: Android, webview user agent vs browser user agent

    Another hint of WebView involvement is presence of X-Requested-With HTTP header with an application package name. Note that this header is also set by XMLHttpRequest, so you need to look for the actual value of the header.

    The statement that WebView doesn't support iframes is not correct.

    0 讨论(0)
  • 2021-01-21 02:27

    The info others provided in this thread gave me what I needed to solve this problem in my case. For others, here is the resulting JS regex which represents the detection described in the accepted answer:

    /(Version\/\d+.*\/\d+.0.0.0 Mobile|; ?wv|(iPhone|iPod|iPad).*AppleWebKit(?!.*Safari))/i.test(navigator.userAgent)
    

    The regex includes cases for old Android, new Android, iOS versions.

    0 讨论(0)
  • 2021-01-21 02:38

    I know how to do it. It is very simple. Because there is an object used by Android web view to trigger functions in its Android app via javascript. So in your js code you can use:

    if (typeof Android === "undefined") {
        // do something if is NOT a web view
    } else {
        // do something else if is a web view
    }
    
    0 讨论(0)
提交回复
热议问题