Marci
Marci

Reputation: 31

Android WebView detection in PHP

I've done a simple Android App with native menu and a webview with my site content. I need to detect if my website is included in a webview in order to hide the menu bar.

After a long research I found this way:

if($_SERVER['HTTP_X_REQUESTED_WITH'] == "myAppPackage"){
    //the site is included in webview
}

This solution is good for a lot of devices, but for a Galaxy S4 Mini(Android 4.2.2) this variable is blank!

Other htp header variables:

Upvotes: 0

Views: 3713

Answers (1)

Marci
Marci

Reputation: 31

Thanks to greenapps idea, this is the final solution.

Android APP MainActivity:

public View onCreateView
....

WebView webView = (WebView) rootView.findViewById(R.id.my_webview);


String agentModified = webView.getSettings().getUserAgentString().concat(" MobileApplication(mypackage)");
webView.getSettings().setUserAgentString(agentModified);

Web site:

if(strpos($_SERVER['HTTP_USER_AGENT'], 'com.vivicrema.android') !== false)
    $isMobileApp = true;

It works like a charm!

Upvotes: 1

Related Questions