Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect webview in Android with Javascript

There is an app on Google Play which is embedding my website in a webview. The app does nothing else, and includes a 3rd party monetization feature.

I want to detect when users are accessing my site via the app, so that I can show a message.

I haven't however been able to find a way to distinguish between the Android mobile browser and the app, as the user agents are the same.

Is there any known method to detect a webview?

Thanks.

like image 951
Nick Avatar asked Jul 22 '13 08:07

Nick


2 Answers

Unfortunately, the answer is no, there isn't a way to distinguish between the Android mobile browser and another webview-based app.

Unless the app developer elects to modify the user agent, of course.

like image 114
stumpped Avatar answered Oct 16 '22 01:10

stumpped


If you know the App ID of the app displaying your website through a webview, you can use PHP to detect if your site is being accessed through the app rather than a mobile browser by using the $_SERVER global array.

<?php

    $app_id = "com.domain.appname";
    $using_app = ( $_SERVER['HTTP_X_REQUESTED_WITH'] == $app_id ) ? TRUE: FALSE;

    if ( $using_app ) {
        echo "Using app message.";
    }

?>

You can then use the $using_app variable to control content output and display a custom message.

This is all assuming of course you are server scripting your site via PHP.

like image 41
McClint Avatar answered Oct 16 '22 02:10

McClint