javascript 您如何在移动 Safari 中检测 3G 与 Wifi 连接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8598065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:57:21  来源:igfitidea点击:

How do you detect 3G vs. Wifi connection on mobile safari?

javascriptiossafariwifi3g

提问by angelfilm entertainment

I am specifically asking about javascript on a mobile webpage, not an objective-c application. Is there something like Apple Reachability for javascript so I can tell when Edge, 3G or Wifi connection is made or changed?

我专门询问移动网页上的 javascript,而不是 Objective-c 应用程序。是否有类似 Apple Reachability for javascript 这样的东西,以便我可以知道何时建立或更改 Edge、3G 或 Wifi 连接?

采纳答案by angelfilm entertainment

Not unless browsers start giving this information away through the DOM which, currently, they don't. There is a navigatorjavascript 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!!).

除非浏览器开始通过 DOM 提供这些信息,而目前他们没有。有一个navigatorjavascript 对象,它包含很多东西,但不是你要找的东西。我最近还在新闻中看到一些移动运营商正在向他们手机上发出的 HTTP 请求添加 cookie。他们被发现将客户 ID 添加到 cookie(巨大的安全漏洞!!)。

Mobile Safari does add some new DOM additionsbut not what you're asking for.

Mobile Safari 确实添加了一些新的DOM 附加功能,但不是您所要求的。

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.

没有这些选项,你就做不到。互联网协议层旨在封装和隐藏底层的细节。您无法检测 Edge、3G 或 Wifi,就像检测电缆、DSL 或更精细的光学器件一样。

Finally, even if you couldget this information, it wouldn't do you any good unless you had details of every single nodein your TCP connection. For example, I could have the following setup :

最后,即使您可以获得此信息,除非您拥有TCP 连接中每个节点的详细信息,否则它对您没有任何好处。例如,我可以进行以下设置:

iPad  ---WiFi---->  iPhone's Hotspot  ---3G--->  Carrier ---unknown--->>>


Addendum

附录

In 2012-2013, the W3Cwas fleshing out The Network Information APIwhich 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!!

2012-2013 年,W3C充实了网络信息 API,旨在提供“一个 Web 应用程序访问设备底层连接信息的接口”当时,API 规定浏览器的“估计”带宽可通过navigator.connection.bandwidth. 截至 2014 年 4 月,该工作已停止!

Update:As of 20th October 2015, work continues on this API. You can find the latest Editor's drafts for the Network Information APIon the W3C's github pages.

更新:截至 2015 年 10 月 20 日,此 API 的工作仍在继续。您可以在 W3C 的 github 页面上找到Network Information API的最新编辑草稿

回答by Collarbone

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.

尽管您无法使用来自浏览器的数据来检测用户是使用 Wifi 还是蜂窝连接,但捕获用户的 IP 可能是区分 Wifi 和蜂窝连接的一种方法。

In this article about how cellular impacts your IP address (http://classroom.synonym.com/wifi-change-ip-17586.html) it states the following:

在这篇关于蜂窝如何影响您的 IP 地址的文章 ( http://classroom.synonym.com/wifi-change-ip-17586.html) 中,它说明了以下内容:

While on Wi-Fi, your device's public IP will match all other computers on your network, and your router assigns a local IP. While using a cellular connection, your cell carrier assigns a public IP. Connecting to the cellular Internet does not use a router, so your device won't have a local IP.

在 Wi-Fi 上,您设备的公共 IP 将匹配您网络上的所有其他计算机,并且您的路由器会分配一个本地 IP。使用蜂窝连接时,您的蜂窝运营商会分配一个公共 IP。连接到蜂窝互联网不使用路由器,因此您的设备将没有本地 IP。

IF ip = localIp, then connection = wifiConnection

如果 ip = localIp,则连接 = wifiConnection

IF ip = publicIp, then connection = cellularConnection

如果 ip = publicIp,则连接 = 蜂窝连接

Here's a screenshot of my IP when I'm on cellular:

这是我使用蜂窝网络时的 IP 屏幕截图:

enter image description here

在此处输入图片说明

Here's a screenshot of my IP (right after turning off cellular) on Wifi:

这是我在 Wifi 上的 IP(关闭蜂窝网络后)的屏幕截图:

enter image description here

在此处输入图片说明

This could be one answer to the problem without browser help.

这可能是没有浏览器帮助的问题的一种答案。

回答by rtrujillor

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

目前这个限制已经有所改善,但不同浏览器(移动或非移动)支持的网络信息 API 仍然存在限制,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-informationthat works like a charm.

如果这对您有帮助,在我们的案例中,由于我们的特定移动应用程序是混合的并且使用 Ionic 开发,我们已经能够使用这个cordova 插件在iOs 或Android 上做到这一点:https: //github.com/apache/cordova-plugin -网络信息,就像一个魅力。