检测用户是否正在使用webview for android / iOS或常规浏览器 [英] Detect if user is using webview for android/iOS or a regular browser
问题描述
如何使用 Android 或 iOS webview 来检测用户是否正在浏览页面?
How to detect if the user is browsing the page using webview for android or iOS?
在stackoverflow上发布了各种解决方案,但我们还没有针对这两种操作系统的防弹解决方案。
There are various solutions posted all over stackoverflow, but we don't have a bulletproof solution yet for both OS.
目标是if语句,例如:
The aim is an if statement, example:
if (android_webview) {
jQuery().text('Welcome webview android user');
} else if (ios_webview) {
jQuery().text('Welcome webview iOS user');
} else if (ios_without_webview) {
// iOS user who's running safari, chrome, firefox etc
jQuery().text('install our iOS app!');
} else if (android_without_webview) {
// android user who's running safari, chrome, firefox etc
jQuery().text('install our android app!');
}
到目前为止我尝试了什么
检测iOS网页视图(来源):
if (navigator.platform.substr(0,2) === 'iP'){
// iOS (iPhone, iPod, iPad)
ios_without_webview = true;
if (window.indexedDB) {
// WKWebView
ios_without_webview = false;
ios_webview = true;
}
}
检测android webview ,我们有许多解决方案,例如这个和这个。我不确定什么是合适的方式,因为每个解决方案似乎都有问题。
Detect android webview, we have a number of solutions like this and this. I'm not sure what's the appropriate way to go because every solution seems to have a problem.
推荐答案
检测iOS设备的浏览器与Android设备不同。对于iOS设备,您可以通过使用 JavaScript:
Detecting browser for iOS devices is different from the Android one. For iOS devices you can do it by checking user agent using JavaScript:
var userAgent = window.navigator.userAgent.toLowerCase(),
safari = /safari/.test( userAgent ),
ios = /iphone|ipod|ipad/.test( userAgent );
if( ios ) {
if ( safari ) {
//browser
} else if ( !safari ) {
//webview
};
} else {
//not iOS
};
对于Android设备,您需要通过服务器端编码来检查请求标头。
For Android devices, you need to do it through server side coding to check for a request header.
PHP:
if ($_SERVER['HTTP_X_REQUESTED_WITH'] == "your.app.id") {
//webview
} else {
//browser
}
JSP:
if ("your.app.id".equals(req.getHeader("X-Requested-With")) ){
//webview
} else {
//browser
}
Ref:detect ipad/iphone webview via javascript
这篇关于检测用户是否正在使用webview for android / iOS或常规浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!