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

后端 未结 10 2200
迷失自我
迷失自我 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 07:52

    This is the extended version of rhavendc's answer. It can be used for showing app install banner when a website is visited from browser, and hiding the banner when a website is opened in a webview.

    $iPhoneBrowser  = stripos($_SERVER['HTTP_USER_AGENT'], "iPhone");
    $iPadBrowser    = stripos($_SERVER['HTTP_USER_AGENT'], "iPad");
    $AndroidBrowser = stripos($_SERVER['HTTP_USER_AGENT'], "Android");
    $AndroidApp = $_SERVER['HTTP_X_REQUESTED_WITH'] == "com.company.app";
    $iOSApp = (strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile/') !== false) && (strpos($_SERVER['HTTP_USER_AGENT'], 'Safari/') == false);
    
    if ($AndroidApp) {
        echo "This is Android application, DONT SHOW BANNER";
    }
    else if ($AndroidBrowser) {
        echo "This is Android browser, show Android app banner";
    }
    else if ($iOSApp) {
        echo "This is iOS application, DONT SHOW BANNER";
    }
    else if($iPhoneBrowser || $iPadBrowser) {
        echo "This is iOS browser, show iOS app banner";
    }
    

提交回复
热议问题