Search code examples
javascriptiosuiwebviewwkwebview

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


I have a browser base game that for some reason doesn't run when shared on for example LinkedIn because it runs in a webView.

How can you detect if you web page is viewed inside a WebView of an app? I especially need a solution for iOS.


Solution

  • Here's a JSFiddle that answers this question:

    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/