检测您的网页是否在应用程序的 WebView 中查看?

Detect if you web page is viewed inside a WebView of an app?

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

如何检测您的网页是否在应用程序的 WebView 中查看?我特别需要 iOS.

的解决方案

这是回答这个问题的 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/