是否可以在 iOS 模拟器中禁用网络?

发布于 2024-10-14 13:52:14 字数 166 浏览 3 评论 0原文

我正在尝试调试在从互联网获取主要数据的应用程序中看到的一些不一致的行为。我在模拟器中没有看到问题,只是在设备上看到问题,因此我想在模拟器中重现网络和连接环境。

有没有办法在模拟器中禁用网络?

(我正在远程连接到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(24

心作怪 2024-10-21 13:52:15

由于 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:
Ethernet Network Source

To turn off you Mac Wi-Fi Internet source (if your Mac is on a Wi-Fi Internet connection):
Enter image description here

剩一世无双 2024-10-21 13:52:15

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

sudo nano /etc/hosts

添加以下行:

192.168.1.123   example.com

或者如果您没有在本地计算机上运行 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.

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.

遗心遗梦遗幸福 2024-10-21 13:52:15

从 Xcode 4(?)开始,/Applications/Utilities 中有一个名为 Network Link Conditioner 的首选项窗格。您可以使用现有配置文件之一,也可以创建自己的自定义配置文件,其上行/下行链路速度为 0 kbit/s,并且掉线率为 100%。

Since Xcode 4 (?) there is a preferences pane in /Applications/Utilities called 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.

方圜几里 2024-10-21 13:52:15

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.

嗼ふ静 2024-10-21 13:52:15

您可以使用第三方应用程序(例如 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.

如歌彻婉言 2024-10-21 13:52:15

您可以使用 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.

柠檬心 2024-10-21 13:52:15

一个可能疯狂的想法或补丁:

只需切换网络可达性标志

这是我用来在运行时通过触发“模拟器内存​​警告”来切换标志的代码,它是完全安全。只需确保代码仅处于调试模式

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

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
}
浪漫人生路 2024-10-21 13:52:15

对于 Xcode 8.3 和 iOS 10.3:

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

请务必在测试套件文件的顶部包含 @available(iOS 10.3, *)

如果您愿意,也可以“打开飞行模式”。

一旦 Siri 关闭 Wi-Fi 或打开飞行模式,您需要关闭表示 Siri 需要互联网的 Siri 对话框。这是通过按主页按钮来完成的,该按钮会关闭对话框并返回到您的应用程序。

With Xcode 8.3 and 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.

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.

叫思念不要吵 2024-10-21 13:52:15

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

You could use OHHTTPStubs and stub the network requests to specific URLs to fail.

小清晰的声音 2024-10-21 13:52:15

一个简单的解决方案是为您的 Mac 创建飞行模式。
操作方法如下:

  • 进入系统偏好设置中的网络,
  • 单击位置下拉菜单,
  • 单击加号图标添加新位置,将
  • 新位置命名为“飞行模式”或类似名称,然后单击“完成”,
  • 选择您刚刚创建的位置位置下拉菜单
  • 依次单击窗口左侧列表中的每个可用网络接口,禁用每个接口
  • 单击配置 iPv4 菜单,然后选择关闭
  • Wi-Fi,只需单击关闭 Wi-Fi 按钮即可
  • 单击“应用”,此位置将阻止所有网络活动

当您想要重新打开网络时,只需从“位置”下拉菜单中选择“自动”,然后单击“应用

A simple solution is to create an Airplane Mode for your 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
  • for Wi-Fi, just click on the Turn Wi-Fi Off button
  • 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

梦在深巷 2024-10-21 13:52:15

您可以在 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.

羅雙樹 2024-10-21 13:52:15

两种方法可以禁用 iOS 模拟器的互联网连接:

  • 拔下网络连接
  • 关闭 Wi-Fi

这是最简单的方法。

There are two ways to disable iOS Simulator's Internet connection:

  • Unplug your network connection
  • Turn Wi-Fi off

It's the simplest way.

泪眸﹌ 2024-10-21 13:52:15

如果您需要通过不同的方式破坏网络连接,请查看这些实用程序:

ipfw 或 pfctl (FreeBSD/OSX) - 低级数据包过滤器。 拒绝从 localhost 访问端口的示例
要禁用网络,您可以在终端中执行:

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

要停止过滤:

pfctl -d

曾经有 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:

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.

≈。彩虹 2024-10-21 13:52:15

在 Mac 中,您可以先在 iOS 模拟器中关闭网络

  1. ,然后进入网络设置
  2. 并断开(禁用)以太网或 wifi

在此处输入图片描述

in Mac, you can turn off the network in iOS Simulator

  1. first go in setting
  2. go in network and disconnect (disable) Ethernet or wifi

enter image description here

黯然 2024-10-21 13:52:15

只需禁用 Macbook 上的互联网连接即可。

Simply disable an internet connection on your Macbook.

笑忘罢 2024-10-21 13:52:15

只需关闭运行模拟器的设备的 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.

孤千羽 2024-10-21 13:52:15

如果你至少有两个 Wi-Fi 网络需要连接,一个非常简单的方法就是利用 iOS 模拟器中的一个 bug:

  1. 退出模拟器 (Cmd + Q)如果它是打开的,
  2. 请将您的 Mac 连接到一个 Wi-Fi 接入点(它可能未连接到互联网,但这并不重要)
  3. 启动模拟器(菜单:Xcode → Open Developer工具iOS 模拟器)并等待加载,
  4. 将 Wi-Fi 网络切换到另一个网络

错误是模拟器尝试使用不支持的网络(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:

  1. quit from the simulator (Cmd + Q) if it is open
  2. connect your Mac to one Wi-Fi access point (it may be not connected to the Internet, but it doesn't matter)
  3. launch the simulator (menu: Xcode → Open Developer TooliOS Simulator) and wait while it is loaded
  4. switch Wi-Fi network to the other one
  5. profit

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.

屋顶上的小猫咪 2024-10-21 13:52:15

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

You could disable the network of the host instead!

风轻花落早 2024-10-21 13:52:14

使用简单的法拉第笼来阻止或限制外部射频信号电平。

您可以用铝箔自己制作。如果您需要的话,开口应小于数据服务的波长阻止。

  • 800 MHz 的波长为 37 cm (14")
  • 1900 MHz 的波长为 16 cm (6")。

这在实际设备上比在模拟器上效果更好,因为 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.

  • 800 MHz has a 37 cm (14") wavelength
  • 1900 MHz has a 16 cm (6") wavelength.

This works better with an actual device than with the simulator since the Mac is hard to work on when inside the Faraday cage ;-)

Enter image description here

栖竹 2024-10-21 13:52:14

是的。在 Xcode 中,您可以进入菜单打开开发者工具更多开发者工具并下载“Xcode 的附加工具",其中包含网络链接调节器。

使用此工具,您可以模拟不同的网络场景(例如 100% 丢失、3G、高延迟 DNS 等),也可以创建自己的自定义场景。

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.

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.

×眷恋的温暖 2024-10-21 13:52:14

恐怕不是——模拟器共享操作系统正在使用的任何网络连接。我不久前提交了一份关于模拟网络状况的雷达错误报告;你可能会考虑这样做。

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.

沙与沫 2024-10-21 13:52:14

据我所知,在 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.

温柔少女心 2024-10-21 13:52:14

下载其他工具包(网络链接调节器)

描述

Sierra 中的示例:
输入图像描述此处

在此处输入图像描述

Download Additional tools package (Network Link Conditioner)

Description

Example in Sierra:
enter image description here

enter image description here

才能让你更想念 2024-10-21 13:52:14

只需在 Mac OS X 中关闭 Wi-Fi 即可。这真是一种享受!

Just turn off your Wi-Fi in Mac OS X. This works a treat!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文