Hello,
I will tell you how to distinguish whether i access mobile web or webview
If you are using a webview, you will want to know the route you accessed.
In general, WebView such as Hybrid App is slower than Native App, and developer sometimes use them to give animation effect such as progress bar
There are many other cases
Like this, Developer distributing hybridApp need to distinguish mobile web and webview for implement function only operates in webview
In Javascript, we know that 'navigator.userAgent;' code informs where you accessed
If you are access in Mobile Android Web, the alert(navigator.userAgent) value executed by web server Javascript is
"Mozilla/5.0 (Linux; Android 8.0.0; SM-G930K Build/R16NW; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/75.0.3770.101 Mobile Safari/537.36"
But unfortunately, The same goes for Android WebView.
So we can't distinguish between web and webview by using only userAgent value
Let's see what about ios.
IOS's navigator.userAgent value is
"Mozilla/5.0 (iPhone CPU iPhone OS 13_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML like Gecko)"
However in IOS WebVeiw, Web and WebView is same result
Android and IOS can not distinguish where we access by userAgent value
So now, I will add specific word to userAgent value of webview to distinguish whether i accessed web or webview.
- For Android
In Android Project
String userAgent = mWebSettings.getUserAgentString();
mWebSettings.setUserAgentString(userAgent+" APP_WISHROOM_Android");
Add above code in your android project's MainActivity.java
so now, you can distinguish in android web and android webview
by using below javascript code in web project
In Web Project
var browserInfo = navigator.userAgent;
if(broswerInfo.indexOf("APP_WISHROOM_Android")>-1){
alert(browserInfo)
}else{
alert(browserInfo)
}
Add above code in your web project
Now I will try to access the web view and the web
In Android Mobile Web
In Android WebView
In Android Webview, Unlike the web, "APP_WISHROOM_Android" is added
- For IOS
Add code,
In Swift Project
Add above code in swift project
You can use evaluateJavaScript method on webView instance, which is the instance that loads webview
In Web Project
Please be careful, "APP_WISHROOM_IOS" word is different with android
now we can see these result,
In Mobile IOS Web
In IOS WebView
In IOS Webview, "APP_WISHROOM_IOS" is added
Now like this way, we can distinguish between mobile web and webview on android and ios respectively
Comments
Post a Comment