是否可以在 iOS 模拟器中禁用网络?
我正在尝试调试在从互联网获取主要数据的应用程序中看到的一些不一致的行为。我在模拟器中没有看到问题,只是在设备上看到问题,因此我想在模拟器中重现网络和连接环境。
有没有办法在模拟器中禁用网络?
(我正在远程连接到 Mac 进行编码,现在没有任何其他选择,因此禁用操作系统网络不是一个选项)。
I am trying to debug some inconsistent behaviour I am seeing in an application that gets its primary data from the Internet. I don't see the issues in the simulator, just on the device, so I'd like to reproduce the network and connectivity environment in the simulator.
Is there a way of disabling the network in the simulator?
(I am connecting to the Mac remotely to code, and there isn't any other choice right now, so disabling the OS network isn't an option).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(24)
由于 Xcode 不提供这样的功能,您肯定会选择一些第三方应用程序/工具。
关闭Mac网络也将有助于关闭iOS模拟器网络。
您可以从系统偏好设置...→网络关闭Mac Internet连接,并关闭所需的网络源。
要关闭 Mac 以太网互联网源:
要关闭 Mac Wi-Fi 互联网源(如果您的 Mac 使用 Wi-Fi 互联网连接):
Since Xcode does not provide such a feature, you will definitely go for some third-party application/ tool.
Turning off the Mac network will also help to turn off the iOS Simulator network.
You can turn off you Mac Internet connection from System Preferences... → Network and turn off the desired network source.
To turn off you Mac Ethernet Internet source:
To turn off you Mac Wi-Fi Internet source (if your Mac is on a Wi-Fi Internet connection):
如果您的应用程序连接到特定域,您只需将其添加到您的
/etc/hosts
文件中,并将其路由到本地网络中不存在的 IP 地址...就像没有任何 Internet 连接或无法访问服务器一样。添加以下行:
或者如果您没有在本地计算机上运行 Web 服务器,则使用
127.0.0.1
。If your app is connecting to a specific domain, you can simply add it to your
/etc/hosts
file and route it to a non-existing IP address in your local network... For the application it will be the same as if there wasn't any Internet connection or the server was not reachable.Add the following line:
Or use
127.0.0.1
if you are not running a web server on your local machine.从 Xcode 4(?)开始,
/Applications/Utilities
中有一个名为Network Link Conditioner
的首选项窗格。您可以使用现有配置文件之一,也可以创建自己的自定义配置文件,其上行/下行链路速度为 0 kbit/s,并且掉线率为 100%。Since Xcode 4 (?) there is a preferences pane in
/Applications/Utilities
calledNetwork Link Conditioner
. Either you use one of the existing profiles or you create your own custom profile with 0 kbit/s up/downlink and 100% dropped.iOS 禁用网络
我建议您在 Mac 上使用 Charles Proxy 应用程序。
它允许您使用专为调整网络连接而创建的带宽限制功能。
启动/停止节流 ⌘ 命令 + T
油门设置... ⌘ command + T + ⇧ shift
*如果您通过
添加预设
创建自己的预设使用带宽 0 和 0 用于下载和上传,您可以模拟没有互联网连接。仅为某些特定主机启用它也非常有用。作为替代方案,您可以禁用 Mac 上的连接,因为来自模拟器的所有流量都通过您的计算机。
iOS disable the network
I would suggest you using the Charles Proxy app on Mac.
It allows you using the Bandwidth Throttle feature that was created just for adjusting the network connection.
Start/Stop Throttling ⌘ command + T
Throttle Settings... ⌘ command + T + ⇧ shift
*If you create you own Preset via
Add Preset
with Bandwidth 0 and 0 for Download and upload, you can simulate no Internet connection. Also it is very useful to enable it only for some specific hosts.As an alternative, you can disable your connection on Mac because all traffic from Simulator go through your computer.
您可以使用第三方应用程序(例如 Charles)来限制互联网连接。
在 Mac 上按 Command + Shift + T 设置限制。
You can throttle the Internet connection with a third-party app such as Charles.
Hit Command + Shift + T on a Mac to set up the throttling.
您可以使用 Little Snitch 切断任何单个进程的网络流量,包括那些在iOS模拟器上运行的。这样您就可以保持互联网连接并断开正在运行的应用程序。
You can use Little Snitch to cut off network traffic to any individual process, including ones that run on the iOS simulator. That way you can keep your Internet connection and disconnect your running app.
一个可能疯狂的想法或补丁:
只需切换网络可达性标志
这是我用来在运行时通过触发“模拟器内存警告”来切换标志的代码,它是完全安全。只需确保代码仅处于调试模式
One probably crazy idea or patch:
Just toggle the flag of network reachability
This is code which I use to toggle my flag at runtime by triggering the 'Simulator Memory Warning' and it's completely safe. Just make sure code should be in Debug mode only
对于 Xcode 8.3 和 iOS 10.3:
请务必在测试套件文件的顶部包含
@available(iOS 10.3, *)
。如果您愿意,也可以“打开飞行模式”。
一旦 Siri 关闭 Wi-Fi 或打开飞行模式,您需要关闭表示 Siri 需要互联网的 Siri 对话框。这是通过按主页按钮来完成的,该按钮会关闭对话框并返回到您的应用程序。
With Xcode 8.3 and iOS 10.3:
Be sure to include
@available(iOS 10.3, *)
at the top of your test suite file.You could alternatively "Turn on Airplane Mode" if you prefer.
Once Siri turns off Wi-Fi or turns on Airplane Mode, you will need to dismiss the Siri dialogue that says that Siri requires internet. This is accomplished by pressing the home button, which dismisses the dialogue and returns to your app.
您可以使用 OHHTTPStubs 并将网络请求存根到特定 URL 以失败。
You could use OHHTTPStubs and stub the network requests to specific URLs to fail.
一个简单的解决方案是为您的 Mac 创建飞行模式。
操作方法如下:
当您想要重新打开网络时,只需从“位置”下拉菜单中选择“自动”,然后单击“应用
A simple solution is to create an Airplane Mode for your Mac.
Here is how to do this:
When you want to turn networking back on, just select Automatic from the Location dropdown menu, and click Apply
您可以在 Mac 上使用网络链接调节器。您可以从 Apple 开发者网站下载它。在我们获得旧版本 Xcode 和 iOS 的地方应该可以使用它。使用此网络调节器,您可以将网络强度从 Wi-Fi 更改为无网络。
此外,当您安装网络调节器时,它会安装在系统首选项中。
You can use the network link conditioner on your Mac. You can download it from the Apple developer website. It should be available where we get the older versions of Xcode and iOS. With this network conditioner you can change the strength of the network from Wi-Fi to no network.
Also, when you install the network conditioner, it gets installed in the system preferences.
有两种方法可以禁用 iOS 模拟器的互联网连接:
这是最简单的方法。
There are two ways to disable iOS Simulator's Internet connection:
It's the simplest way.
如果您需要通过不同的方式破坏网络连接,请查看这些实用程序:
ipfw 或 pfctl (FreeBSD/OSX) - 低级数据包过滤器。 拒绝从 localhost 访问端口的示例
要禁用网络,您可以在终端中执行:
要停止过滤:
曾经有 Throttle (Mac)通过共享无线网络模拟3G网络。
If you need to spoil your network connection is different ways take a look at these utils:
ipfw or pfctl (FreeBSD/OSX) - low level packet filter. Sample of denying access to a port from localhost
To disable the network you can execute in terminal:
To stop filtering:
There once was Throttle (Mac) for simulation 3G network through shared wireless network.
在 Mac 中,您可以先在 iOS 模拟器中关闭网络
in Mac, you can turn off the network in iOS Simulator
只需禁用 Macbook 上的互联网连接即可。
Simply disable an internet connection on your Macbook.
只需关闭运行模拟器的设备的 WiFi 即可。这也导致我的 iOS 模拟器失去了 WiFi 连接。现在我可以测试我的应用程序在没有 WiFi 的情况下如何工作。
Just simply turn off the WiFi of the Device you are running the Simulator on. This also made my iOS Simulator loose the WiFi Connection. Now I can test my App how it works without WiFi.
如果你至少有两个 Wi-Fi 网络需要连接,一个非常简单的方法就是利用 iOS 模拟器中的一个 bug:
错误是模拟器尝试使用不支持的网络(IP?)已经连接了。
在您重新启动模拟器之前 - 它将没有互联网连接(即使您连接的第一个 Wi-Fi 网络有互联网连接),因此您可以运行 (Cmd + R) 并停止 (Cmd + .) 项目以在没有连接的情况下使用模拟器,但您的 Mac 将会连接。
然后,如果您需要运行连接的模拟器 - 只需退出并启动它即可。
If you have at least two Wi-Fi networks to connect is a very simple way is to use a bug in iOS simulator:
The bug is that the simulator tries to use a network (IP?) which is not connected already.
Until you relaunched the simulator - it will have no Internet connection (even if that first Wi-Fi network you connected had an Internet connection), so you can run (Cmd + R) and stop (Cmd + .) project(s) to use the simulator without a connection, but your Mac will be connected.
Then, if you'll need to run the simulator connected - just quit and launch it.
您可以改为禁用主机的网络!
You could disable the network of the host instead!
使用简单的法拉第笼来阻止或限制外部射频信号电平。
您可以用铝箔自己制作。如果您需要的话,开口应小于数据服务的波长阻止。
这在实际设备上比在模拟器上效果更好,因为 Mac 在法拉第笼内时很难工作;-)
Use a simple Faraday cage to block or limit the external RF signal level.
You can make your own with aluminum foil. The openings should be smaller than the wavelength of your data service if that's what you want to block.
This works better with an actual device than with the simulator since the Mac is hard to work on when inside the Faraday cage ;-)
是的。在 Xcode 中,您可以进入菜单打开开发者工具 → 更多开发者工具并下载“Xcode 的附加工具",其中包含网络链接调节器。
使用此工具,您可以模拟不同的网络场景(例如 100% 丢失、3G、高延迟 DNS 等),也可以创建自己的自定义场景。
Yes. In Xcode, you can go to menu Open Developer Tools → More Developer Tools and download "Additional Tools for Xcode", which will have the Network Link Conditioner.
Using this tool, you can simulate different network scenarios (such as 100% loss, 3G, high-latency DNS, and more) and you can create your own custom ones as well.
恐怕不是——模拟器共享操作系统正在使用的任何网络连接。我不久前提交了一份关于模拟网络状况的雷达错误报告;你可能会考虑这样做。
I'm afraid not—the simulator shares whatever network connection the OS is using. I filed a Radar bug report about simulating network conditions a while back; you might consider doing the same.
据我所知,在 iOS 模拟器上禁用网络的唯一方法是使用 Little Snitch< 之类的工具/a> 或放手。使用它们,您可以拒绝/阻止任何传出和传入的网络连接。
您可以将其设置为仅阻止来自模拟器应用程序的连接。它的工作原理类似于防火墙。
The only way to disable the network on the iOS simulator I know is using tools like Little Snitch or Hands Off. With them you can deny/block any outgoing and ingoing network connections.
You can set it up so that it only blocks connections from the simulator app. It works like a firewall.
下载其他工具包(网络链接调节器)
描述
Sierra 中的示例:
Download Additional tools package (Network Link Conditioner)
Description
Example in Sierra:
只需在 Mac OS X 中关闭 Wi-Fi 即可。这真是一种享受!
Just turn off your Wi-Fi in Mac OS X. This works a treat!