可以在 javascript 中检测到网络应用程序或移动浏览器

possible to detect webapp or mobile browser in javascript

我正在使用 Web 应用程序 (iOS/Android)

基本上,我的应用程序看起来像

我想做的是检测来自移动应用程序或移动浏览器的用户请求。(使用 javascript)

这可能吗? 如果是,推荐方法(我更喜欢原生javascript因为项目是商业的)

谢谢。

P.S。我不需要使用 navigator.userAgent 进行设备检测,这项工作的目的是检测移动浏览器或移动应用程序,而不是用户使用的设备。

您可以为您的应用程序 webview 添加自定义用户代理字符串,以便您可以区分 webview 和 js 对于 android 你可以使用

myWebView.getSettings().setUserAgentString(<your custom user agent string>);

最好将额外的字符串附加到实际的用户代理字符串

myWebView.getSettings.getSettings().setUserAgentString(myWebView.getSettings().getUserAgentString() + <your custom user agent string>);

您可以检查用户代理字符串是否包含来自 JavaScript 的自定义额外字符串。