Detect if user is using webview for android/iOS or a regular browser

后端 未结 10 2154
迷失自我
迷失自我 2020-11-28 07:27

How to detect if the user is browsing the page using webview for android or iOS?

There are various soluti

相关标签:
10条回答
  • 2020-11-28 08:06

    I came to decision, that instead of detection, more efficient solution simply specify platform in AppConfig.

    const appConfig = {
       appID: 'com.example.AppName.Web'
       //or
       //appID: 'com.example.AppName.MobileIOS'
       //or
       //appID: 'com.example.AppName.MobileAndroid'
    }
    
    0 讨论(0)
  • 2020-11-28 08:16

    Note: This solution is PHP-based. HTTP headers can be faked so this is not the nicest solution but you can have a start with this.

    //For iOS
    
    if ((strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile/') !== false) && (strpos($_SERVER['HTTP_USER_AGENT'], 'Safari/') == false) {
        echo 'WebView';
    } else{
        echo 'Not WebView';
    }
    
    //For Android
    
    if ($_SERVER['HTTP_X_REQUESTED_WITH'] == "com.company.app") {
        echo 'WebView';
    } else{
        echo 'Not WebView';
    }
    
    0 讨论(0)
  • 2020-11-28 08:16

    If you're using Flask (and not PHP), you can check to see if "wv" is in the string for the "User-Agent" header. Feel free to edit / add a more precise way of checking it, but basically "wv" is put there if it's a web view; otherwise, it is not there.

    user_agent_header = request.headers.get('User-Agent', None)
    is_web_view = "wv" in str(header_request) if user_agent_header is not None else False
    

    Chrome Developer Docs:

    WebView UA in Lollipop and Above

    In the newer versions of WebView, you can differentiate the WebView by looking for the wv field as highlighted below.

    Mozilla/5.0 (Linux; Android 5.1.1; Nexus 5 Build/LMY48B; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.65 Mobile Safari/537.36

    0 讨论(0)
  • 2020-11-28 08:17

    Detecting browser for iOS devices is different from the Android one. For iOS devices you can do it by checking user agent using JavaScript:

    var userAgent = window.navigator.userAgent.toLowerCase(),
        safari = /safari/.test( userAgent ),
        ios = /iphone|ipod|ipad/.test( userAgent );
    
    if( ios ) {
        if ( safari ) {
            //browser
        } else if ( !safari ) {
            //webview
        };
    } else {
        //not iOS
    };
    

    For Android devices, you need to do it through server side coding to check for a request header.

    PHP:

    if ($_SERVER['HTTP_X_REQUESTED_WITH'] == "your.app.id") {
        //webview
    } else {
        //browser
    }
    

    JSP:

    if ("your.app.id".equals(req.getHeader("X-Requested-With")) ){
        //webview
    } else {
        //browser
    }
    

    Ref:detect ipad/iphone webview via javascript

    0 讨论(0)
提交回复
热议问题