简体   繁体   English

是否可以在 iOS 模拟器中禁用 .network?

[英]Is it possible to disable the network in iOS Simulator?

I am trying to debug some inconsistent behaviour I am seeing in an application that gets its primary data from the Inte.net.我正在尝试调试我在从 Inte.net 获取其主要数据的应用程序中看到的一些不一致的行为。 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?有没有办法在模拟器中禁用 the.network?

(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). (我正在远程连接到 Mac 进行编码,目前没有任何其他选择,因此禁用 OS.network 不是一个选项)。

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.如果这是您想要阻止的,则开口应小于数据服务的波长

  • 800 MHz has a 37 cm (14") wavelength 800 MHz 具有 37 cm (14") 波长
  • 1900 MHz has a 16 cm (6") wavelength. 1900 MHz 的波长为 16 cm (6")。

This works better with an actual device than with the simulator since the Mac is hard to work on when inside the Faraday cage ;-)这在实际设备上比在模拟器上更有效,因为 Mac 在法拉第笼内很难工作;-)

在此处输入图片说明

Yes.是的。 In Xcode, you can go to menu Open Developer ToolsMore Developer Tools and download " Additional Tools for Xcode ", which will have the Network Link Conditioner.在 Xcode 中,您可以转到菜单Open Developer ToolsMore Developer Tools并下载“ Additional Tools for Xcode ”,其中将包含 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.使用此工具,您可以模拟不同的网络场景(例如 100% 丢失、3G、高延迟 DNS 等),您也可以创建自己的自定义场景。

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;不久前,我提交了一份关于模拟网络条件的Radar 错误报告 you might consider doing the same.你可能会考虑做同样的事情。

The only way to disable the network on the iOS simulator I know is using tools like Little Snitch or Hands Off .我知道在 iOS 模拟器上禁用网络的唯一方法是使用Little SnitchHands 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.它像防火墙一样工作。

只需在 Mac OS X 中关闭您的 Wi-Fi。这很管用!

Download Additional tools package (Network Link Conditioner)下载附加工具包(网络链接调节器)

Description描述

Example in Sierra: Sierra中的示例: 在此处输入图片说明

在此处输入图片说明

Since Xcode 4 (?) there is a preferences pane in /Applications/Utilities called Network Link Conditioner .从 Xcode 4 (?) 开始, /Applications/Utilities有一个名为Network 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.您可以使用现有配置文件之一,也可以使用 0 kbit/s 上行/下行链路和 100% 下降创建自己的自定义配置文件。

Since Xcode does not provide such a feature, you will definitely go for some third-party application/ tool.由于 Xcode 不提供这样的功能,您肯定会选择一些第三方应用程序/工具。 Turning off the Mac network will also help to turn off the iOS Simulator network.关闭 Mac 网络也有助于关闭 iOS Simulator 网络。

You can turn off you Mac Internet connection from System Preferences...Network and turn off the desired network source.您可以从系统偏好设置...网络关闭 Mac 互联网连接并关闭所需的网络源。

To turn off you Mac Ethernet Internet source:要关闭 Mac 以太网 Internet 源: 以太网网络源

To turn off you Mac Wi-Fi Internet source (if your Mac is on a Wi-Fi Internet connection):要关闭 Mac Wi-Fi Internet 源(如果您的 Mac 使用 Wi-Fi Internet 连接): 在此处输入图片说明

You can throttle the Internet connection with a third-party app such as Charles .您可以使用Charles等第三方应用程序限制 Internet 连接。

Hit Command + Shift + T on a Mac to set up the throttling.在 Mac 上按Command + Shift + T来设置节流。

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只需确保代码应仅处于调试模式

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
#ifdef DEBUG
    isInternetAvailable = !isInternetAvailable;
#endif
}

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.如果您的应用程序连接到特定域,您只需将其添加到/etc/hosts文件并将其路由到本地网络中不存在的 IP 地址...没有任何 Internet 连接或无法访问服务器。

sudo nano /etc/hosts

Add the following line:添加以下行:

192.168.1.123   example.com

Or use 127.0.0.1 if you are not running a web server on your local machine.或者,如果您没有在本地计算机上运行 Web 服务器,请使用127.0.0.1

You can use Little Snitch to cut off network traffic to any individual process, including ones that run on the iOS simulator.您可以使用Little Snitch切断任何单个进程的网络流量,包括在 iOS 模拟器上运行的进程。 That way you can keep your Internet connection and disconnect your running app.这样您就可以保持 Internet 连接并断开正在运行的应用程序的连接。

I would suggest you using the Charles Proxy app on Mac.我建议您在 Mac 上使用Charles Proxy应用程序。

It allows you using the Bandwidth Throttle feature that was created just for adjusting the network connection.它允许您使用仅为调整网络连接而创建的带宽限制功能。

Start/Stop Throttling ⌘ command + T开始/停止节流⌘ 命令+ T
Throttle Settings... ⌘ command + T + ⇧ shift油门设置... ⌘ 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. *如果您通过为下载和上传Add Preset带宽为 0 和 0 的预设来创建自己的预设,则可以模拟没有 Internet 连接。 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.作为替代方案,您可以在 Mac 上禁用连接,因为来自模拟器的所有流量都通过您的计算机。

With Xcode 8.3 and iOS 10.3:使用 Xcode 8.3 和 iOS 10.3:

XCUIDevice.shared().siriService.activate(voiceRecognitionText: "Turn off Wi-Fi")
XCUIDevice.shared().press(XCUIDeviceButton.home)

Be sure to include @available(iOS 10.3, *) at the top of your test suite file.确保在测试套件文件的顶部包含@available(iOS 10.3, *)

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.一旦Siri关闭 Wi-Fi 或打开飞行模式,您将需要关闭 Siri 对话,即 Siri 需要互联网。 This is accomplished by pressing the home button, which dismisses the dialogue and returns to your app.这是通过按下主页按钮来完成的,这会关闭对话并返回到您的应用程序。

There are two ways to disable iOS Simulator's Internet connection:两种方法可以禁用iOS 模拟器的 Internet 连接:

  • Unplug your network connection拔掉网络连接
  • Turn Wi-Fi off关闭 Wi-Fi

It's the simplest way.这是最简单的方法。

您可以使用OHHTTPStubs并将网络请求存根到特定 URL 以失败。

A simple solution is to create an Airplane Mode for your Mac.一个简单的解决方案是为您的 Mac 创建飞行模式。 Here is how to do this:以下是如何执行此操作:

  • go into Network in System Preferences在系统偏好设置中进入网络
  • click on Location dropdown menu单击位置下拉菜单
  • click on plus icon to add a new location单击加号图标以添加新位置
  • name the new location 'Airplane Mode' or similar, and click 'Done'将新位置命名为“飞行模式”或类似名称,然后单击“完成”
  • select the location you just created from the Location dropdown menu从位置下拉菜单中选择您刚刚创建的位置
  • click on each available network interface in the list at the left of the window in turn, disabling each one依次单击窗口左侧列表中的每个可用网络接口,禁用每个接口
  • click on the Configure iPv4 menu, and choose Off单击配置 iPv4 菜单,然后选择关闭
  • for Wi-Fi, just click on the Turn Wi-Fi Off button对于 Wi-Fi,只需单击关闭 Wi-Fi 按钮
  • click Apply, and this location will block all network activity单击应用,此位置将阻止所有网络活动

When you want to turn networking back on, just select Automatic from the Location dropdown menu, and click Apply当您想重新打开网络时,只需从位置下拉菜单中选择自动,然后单击应用

If you have at least two Wi-Fi networks to connect is a very simple way is to use a bug in iOS simulator:如果您至少有两个 Wi-Fi 网络要连接,一个非常简单的方法是使用 iOS 模拟器中的一个错误:

  1. quit from the simulator ( Cmd + Q ) if it is open退出模拟器( Cmd + Q ),如果它是打开的
  2. connect your Mac to one Wi-Fi access point (it may be not connected to the Internet, but it doesn't matter)将你的 Mac 连接到一个 Wi-Fi 接入点(它可能没有连接到互联网,但没关系)
  3. launch the simulator (menu: Xcode → Open Developer TooliOS Simulator ) and wait while it is loaded启动模拟器(菜单:Xcode → Open Developer TooliOS Simulator )并等待加载
  4. switch Wi-Fi network to the other one将 Wi-Fi 网络切换到另一个网络
  5. profit利润

The bug is that the simulator tries to use a network (IP?) which is not connected already.错误在于模拟器尝试使用尚未连接的网络(IP?)。

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.在您重新启动模拟器之前 - 它不会有互联网连接(即使您连接的第一个 Wi-Fi 网络有互联网连接),因此您可以运行( Cmd + R )和停止( Cmd + . )项目以在没有连接的情况下使用模拟器,但您的 Mac 将被连接。

Then, if you'll need to run the simulator connected - just quit and launch it.然后,如果您需要运行连接的模拟器 - 只需退出并启动它。

You can use the network link conditioner on your Mac.您可以在 Mac 上使用网络链接调节器。 You can download it from the Apple developer website.您可以从 Apple 开发者网站下载。 It should be available where we get the older versions of Xcode and iOS.它应该可以在我们获得旧版本的 Xcode 和 iOS 的地方使用。 With this network conditioner you can change the strength of the network from Wi-Fi to no network.使用此网络调节器,您可以将网络强度从 Wi-Fi 更改为无网络。

Also, when you install the network conditioner, it gets installed in the system preferences.此外,当您安装网络调节器时,它会安装在系统首选项中。

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. ipfw 或 pfctl (FreeBSD/OSX) - 低级数据包过滤器。 Sample of denying access to a port from localhost To disable the.network you can execute in terminal: 拒绝从本地主机访问端口的示例要禁用 .network,您可以在终端中执行:

pfctl -e
(echo "block all"; pfctl -sr) | pfctl -f -

To stop filtering:要停止过滤:

pfctl -d

There once was Throttle (Mac) for simulation 3G.network through shared wireless.network.曾经有Throttle (Mac)通过共享无线网络模拟3G网络。

您可以改为禁用主机的网络!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM