Reputation: 16842
Me and my team are going to develop this native Android app which will be basically a WebView wrapper for a specific mobile website, implementing a couple of native functionality.
There will be a banner on the website to advertise the native app. But it doesn't make any sense to display this banner when the user is already using the native app, so we have to get rid of it.
My first approach is to inject some Javascript into the WebView which will look for the HTML element and change it's visibility. Simple enough. But is this the best/recommended approach for such thing?
Maybe it would be better if the website developers were to detect if the the website is being displayed through the native app and simply don't display the banner. Yes? How would you suggest to do this?
Upvotes: 0
Views: 806
Reputation: 6073
There are a couple of approaches...
You can ask the main site's developers to create a Javascript function called hideAd()
, and then you can call that function once the page is loaded using webView.loadUrl("javascript:hideAd()")
.
You can create an interface that exists only in the native app, that the webpage can query to determine if it's running in the app. Use webView.addJavascriptInterface(new MyJSJavaBridge(), "nativeapp");
on the Java side and then check for the interface in Javascript with if (window.nativeapp)...
.
You can change the user agent string to append some unique identifier for your app using webView.getSettings().setUserAgentString("<old agent string plus some custom id>");
, and then have the JS code query the user agent string.
There are probably many other options, these are just some of them. I'd say the easiest is probably appending some unique id to the user agent.
Upvotes: 1