【问题标题】:Detect if you web page is viewed inside a WebView of an app?检测您的网页是否在应用程序的 WebView 中查看?
【发布时间】:2019-05-18 15:56:10
【问题描述】:

我有一个基于浏览器的游戏,由于某种原因在 LinkedIn 上共享时无法运行,因为它在 webView 中运行。

如何检测您的网页是否在应用的 WebView 中查看?我特别需要 iOS 的解决方案。

【问题讨论】:

  • WebView 发送带有应用程序名称和版本的用户代理。不是吗?
  • 我不是应用开发者所以我不知道。
  • 如果你是 web 开发者,你可以通过检查 User-Agent 来验证它。)
  • 返回“Mozilla/5.0 (Linux; Android 9; Pixel XL) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.83 Mobile Safari/537.36”

标签: javascript ios uiwebview wkwebview


【解决方案1】:

这是一个回答这个问题的 JSFiddle:

var standalone = window.navigator.standalone,
        userAgent = window.navigator.userAgent.toLowerCase(),
        safari = /safari/.test( userAgent ),
        ios = /iphone|ipod|ipad/.test( userAgent );

if( ios ) {

        if ( !standalone && safari ) {

                document.getElementById( 'where-am-i' ).textContent = 'browser';

        } else if ( standalone && !safari ) {

                document.getElementById( 'where-am-i' ).textContent = 'standalone';

        } else if ( !standalone && !safari ) {

                document.getElementById( 'where-am-i' ).textContent = 'uiwebview';

        };

} else {

        document.getElementById( 'where-am-i' ).textContent = 'not iOS';

};

https://jsfiddle.net/ThinkingStiff/6qrbn/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-18
    • 2013-12-09
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 2011-06-24
    • 2022-01-06
    • 1970-01-01
    相关资源
    最近更新 更多