如何在 Mobile Safari 中检测 3G 与 Wifi 连接?
我特别询问的是移动网页上的 javascript,而不是 Objective-C 应用程序。是否有像 Apple Reachability for javascript 这样的东西,以便我可以知道 Edge、3G 或 Wifi 连接何时建立或更改?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
除非浏览器开始通过 DOM 泄露这些信息,但目前他们还没有这样做。有一个
navigator
javascript 对象,它包含很多内容,但不是您要查找的内容。我最近还在新闻中读到,一些移动运营商正在向其手机上发出的 HTTP 请求添加 cookie。他们被发现将客户 ID 添加到 cookie(巨大的安全漏洞!!)。Mobile Safari 确实添加了一些新的 DOM 添加,但不是您所要求的。
缺少这些选项,您就不能。互联网协议层旨在封装和传输数据。隐藏底层的细节。您无法检测 Edge、3G 或 Wifi,就像无法检测电缆、DSL 或更精细的光学器件一样。
最后,即使您能够获得此信息,除非您拥有 TCP 连接中每个节点的详细信息,否则它对您没有任何好处。例如,我可以进行以下设置:
附录
在 2012-2013 年,W3C 充实了 网络信息 API 旨在提供“一个供 Web 应用程序访问的接口”设备的底层连接信息”。 当时,API规定浏览器的“估计”带宽可以通过 navigator.connection.bandwidth 获得。 截至 2014 年 4 月,该工作已停止!
更新 1: 截至 2015 年 10 月 20 日,该 API 的工作仍在继续。您可以在 W3C 的 github 页面上找到最新的网络信息 API 的编辑草稿。
更新 2:2020 年 6 月,出于隐私问题,Apple 拒绝在 Safari 中实现网络信息 API
Not unless browsers start giving this information away through the DOM which, currently, they don't. There is a
navigator
javascript objects which holds many things but not what you're looking for. I've also read in the news recently that some cellular carrier was adding cookies to HTTP requests made on their phones. They were found to be adding customer IDs to cookies (huge security breach!!).Mobile Safari does add some new DOM additions but not what you're asking for.
Short of these options, you can't. Layers of the Internet Protocol are meant to encapsulate & hide the details of the bottom layers. You can't detect Edge, 3G or Wifi any more than you can detect cable, DSL or finer optics.
Finally, even if you could get this information, it wouldn't do you any good unless you had details of every single node in your TCP connection. For example, I could have the following setup :
Addendum
In 2012-2013, the W3C was fleshing out The Network Information API which was aimed at providing "an interface for web applications to access the underlying connection information of the device". At the time, the API stipulated that an "estimated" bandwidth for the browser would be obtainable via
navigator.connection.bandwidth
. As of April 2014, that work has since been discontinued!!Update 1: As of 20th October 2015, work continues on this API. You can find the latest Editor's drafts for the Network Information API on the W3C's github pages.
Update 2: In June 2020, Apple declined to implement the Network Information API in Safari due to privacy concerns
尽管您无法使用浏览器中的数据来检测用户是使用 Wifi 还是蜂窝连接,但捕获用户的 IP 可能是区分 Wifi 和蜂窝连接的一种方法。
在这篇关于蜂窝网络如何影响您的 IP 地址的文章中 (http://classroom.synonym .com/wifi-change-ip-17586.html)它指出以下内容:
IF ip = localIp,则连接 = wifiConnection
IF ip = publicIp,则连接 = CellularConnection
这是我使用蜂窝网络时 IP 的屏幕截图:
这是我在 Wifi 上的 IP 屏幕截图(关闭蜂窝网络后立即):
这可能是在没有浏览器帮助的情况下解决问题的一种答案。
Although you cannot use data from a browser to detect if the user is on Wifi or a cellular connection - capturing the user's IP may be one way to differentiate between a Wifi and Cellular connection.
In this article about how cellular impacts your IP address (http://classroom.synonym.com/wifi-change-ip-17586.html) it states the following:
IF ip = localIp, then connection = wifiConnection
IF ip = publicIp, then connection = cellularConnection
Here's a screenshot of my IP when I'm on cellular:
Here's a screenshot of my IP (right after turning off cellular) on Wifi:
This could be one answer to the problem without browser help.
目前此限制已得到改善,但不同浏览器(移动或非移动)支持的网络信息 API 仍然存在限制,https://caniuse.com/#search=network
如果这对您有帮助,在我们的例子中,由于我们的特定移动应用程序是混合的并使用 Ionic 开发,我们已经能够在任一 iO 上做到这一点或 Android 使用此 cordova 插件: https://github.com/apache/cordova-plugin-network-information 这就像一个魅力。
At the moment this limitation has improved, but there are still restrictions with the Network Information API supported in the different browsers ( mobile or not ), https://caniuse.com/#search=network
If this helps you, In our case as our specific mobile application is hybrid and developed using Ionic, we have been able to do it on either iOs or Android using this cordova plugin: https://github.com/apache/cordova-plugin-network-information that works like a charm.