检测在应用内浏览器(web视图)与PHP / Java脚本 [英] Detect in-app browser (WebView) with PHP / Javascript
问题描述
我开发的iOS和Android的应用程序使用在线应用程序的浏览器(web视图)的访问HTML文件从我的网络服务器。
I developed an app for iOS and Android which accesses an HTML file from my webserver using the in-app browser (Webview).
我不希望用户可以访问此文件,而无需使用的应用程序。是否有可能进行检测,如果用户与应用程序或者直接经由浏览器访问该智能电话/平板/计算机上的文件?我认为,随着PHP的解决方案要好得多,因为JavaScript可以关掉。至少谷歌Analytics(分析)可以Safari浏览器和Safari(在应用程序)之间的区别。它应与每一个版本的iOS和Android。
I don't want that a user can access this file without using the app. Is there a possibility to detect, if the user is accessing the file with the app or directly via a browser on this smartphone / tablet / computer? I think that a solution with PHP is much better, because Javascript can be switched off. At least Google Analytics can differentiate between Safari and Safari (in-app). It should work with every version of iOS and Android.
感谢您的帮助。
解决方案
多次尝试后,我终于找到了一个解决方案,工作对我来说!
After many attempts I finally found a working solution for me!
iOS的: 则可以检测Safari和使用用户代理应用程序内的浏览器之间的差异。也许有一个更好的解决方案,但它的工作原理。
iOS: You can detect the difference between Safari and the in-app browser using the user agent. Probably there's a nicer solution, but it works.
// Safari (in-app)
if ((strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile/') !== false) && (strpos($_SERVER['HTTP_USER_AGENT'], 'Safari/') == false) {
echo 'Safari (in-app)';
}
机器人:
从应用程序的软件包名称存储在PHP变量 $ _ SERVER ['HTTP_X_REQUESTED_WITH']
。
Android:
The package name from the app is stored in the PHP variable $_SERVER['HTTP_X_REQUESTED_WITH']
.
// Android (in-app)
if($_SERVER['HTTP_X_REQUESTED_WITH'] == "com.company.app") {
echo 'Android (in-app)';
}
由于蒂姆·范Elsloo已经指出的HTTP标头可以伪造,这也不是绝对安全的。
As Tim van Elsloo already noted HTTP headers can be faked and this is not absolutely secure.
推荐答案
我不知道关于Android,但是当你使用的iOS SDK的的UIWebView
,它发送名称和版本您的应用程序为用户代理的一部分( YourApp / 1.0
)。
I'm not sure about Android, but when you're using the iOS SDK's UIWebView
, it sends the name and version of your app as part of the user agent (YourApp/1.0
).
然后,您可以用PHP来检查,如果你在应用程序的WebView正在使用或不:
You can then use PHP to check if your in-app webview is being used or not:
if (strpos($_SERVER['HTTP_USER_AGENT'], 'YourApp/') !== false)
我认为Android做类似的东西为好。
I think Android does something similar as well.
这篇关于检测在应用内浏览器(web视图)与PHP / Java脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!