xcode 如何监控从 iOS 模拟器发出的网络调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11128362/
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-09-09 04:26:29  来源:igfitidea点击:

How to monitor network calls made from iOS Simulator

xcodenetworkingios-simulatornetwork-monitoring

提问by Mad Echet

I am trying to monitor calls from an app to my server just like Firebug does. I could not find a way to see that in iOS Simulator or in xCode.

我正在尝试像 Firebug 一样监视从应用程序到我的服务器的调用。我找不到在 iOS 模拟器或 xCode 中看到它的方法。

Is there a way to do that without sniffing all the traffic? If no, what tool would you suggest?

有没有办法在不嗅探所有流量的情况下做到这一点?如果没有,你会推荐什么工具?

采纳答案by Macmade

Personally, I use Charlesfor that kind of stuff.
When enabled, it will monitor every network request, displaying extended request details, including support for SSL and various request/reponse format, like JSON, etc...

就个人而言,我使用Charles来处理这类事情。
启用后,它将监视每个网络请求,显示扩展的请求详细信息,包括对 SSL 和各种请求/响应格式的支持,如 JSON 等...

You can also configure it to sniff only requests to specific servers, not the whole traffic.

您还可以将其配置为仅嗅探对特定服务器的请求,而不是整个流量。

It's commercial software, but there is a trial, and IMHO it's definitively a great tool.

它是商业软件,但有一个试用版,恕我直言,它绝对是一个很棒的工具。

回答by mpontillo

A man-in-the-middle proxy, like suggested by other answers, is a good solution if you only want to see HTTP/HTTPS traffic. Burp Suiteis pretty good. It may be a pain to configure though. I'm not sure how you would convince the simulator to talk to it. You might have to set the proxy on your local Mac to your instance of a proxy server in order for it to intercept, since the simulator will make use of your local Mac's environment.

如果您只想查看 HTTP/HTTPS 流量,就像其他答案所建议的那样,中间人代理是一个很好的解决方案。Burp Suite还不错。不过配置起来可能会很痛苦。我不确定您将如何说服模拟器与之交谈。您可能必须将本地 Mac 上的代理设置为代理服务器的实例,以便它进行拦截,因为模拟器将使用您本地 Mac 的环境。

The best solution for packet sniffing (though it only works for actualiOS devices, not the simulator) I've found is to use rvictl. This blog posthas a nice writeup. Basically you do:

我发现的数据包嗅探的最佳解决方案(尽管它仅适用于实际的iOS 设备,而不适用于模拟器)是使用rvictl. 这篇博文写得很好。基本上你这样做:

rvictl -s <iphone-uid-from-xcode-organizer>

Then you sniff the interface it creates with with Wireshark (or your favorite tool), and when you're done shut down the interface with:

然后你用 Wireshark(或你最喜欢的工具)嗅探它创建的界面,完成后关闭界面:

rvictl -x <iphone-uid-from-xcode-organizer>

This is nice because if you want to packet sniff the simulator, you're having to wade through traffic to your local Mac as well, but rvictlcreates a virtual interface that just shows you the traffic from the iOS device you've plugged into your USB port.

这很好,因为如果您想对模拟器进行数据包嗅探,您还必须通过流量到本地 Mac,但rvictl会创建一个虚拟界面,只显示您插入 USB 的 iOS 设备的流量港口。

回答by giff

A free and open source proxy tool that runs easily on a Mac is mitmproxy.

可以在 Mac 上轻松运行的免费开源代理工具是mitmproxy

The website includes links to a Mac binary, as well as the source code on Github.

该网站包含指向 Mac 二进制文件的链接,以及 Github 上的源代码。

The docs contain a very helpful intro to loading a cert into your test device to view HTTPS traffic.

这些文档包含一个非常有用的介绍,可以将证书加载到您的测试设备中以查看 HTTPS 流量。

Not quite as GUI-tastic as Charles, but it does everything I need and its free and maintained. Good stuff, and pretty straightforward if you've used some command line tools before.

不像 Charles 那样具有 GUI 风格,但它可以完成我需要的一切,而且它是免费且维护的。好东西,如果您以前使用过一些命令行工具,那就很简单了。

UPDATE: I just noticed on the website that mitmproxy is available as a homebrew install. Couldn't be easier.

更新:我刚刚在网站上注意到 mitmproxy 可以作为自制软件安装使用。再简单不过了。

回答by Andres Paladines

Recently i found a git repothat makes it easy.

最近我发现了一个git repo,它使它变得容易。

You can try it.

你可以试试看。

This is an app's screenshot: enter image description here

这是一个应用程序的屏幕截图: 在此处输入图片说明

Best regards.

此致。

回答by Evan

Xcode provides CFNetwork Diagnostic Logging. Apple doc

Xcode 提供了 CFNetwork 诊断日志。苹果文档

To enable it, add CFNETWORK_DIAGNOSTICS=3in the Environment Variable section:

要启用它,请CFNETWORK_DIAGNOSTICS=3在环境变量部分添加:

enter image description here

在此处输入图片说明

This will show requests from the App with its headers & body. Note that OS_ACTIVITY_MODEmust be set to enableas shown. Otherwise no output will be shown on the Console.

这将显示来自应用程序的请求及其标题和正文。注意OS_ACTIVITY_MODE必须设置enable为如图所示。否则控制台上不会显示任何输出。

回答by cyber8200

Wireshark it

Wireshark它

Select your interface

选择您的界面

enter image description here

在此处输入图片说明

Add filterstart the capture

添加filter启动capture

enter image description here

在此处输入图片说明



Testing

测试

Click on any action or button that would trigger a GET/POST/PUT/DELETE request

单击会触发 GET/POST/PUT/DELETE 请求的任何操作或按钮

You will see it on listed in the wireshark

你会在wireshark中看到它

enter image description here

在此处输入图片说明

If you want to know more details about one specific packet, just select it and Follow > HTTP Stream.

如果您想了解有关某个特定数据包的更多详细信息,只需选择它并点击Follow > HTTP Stream 即可

enter image description here

在此处输入图片说明

hope this help others !!

希望这对其他人有帮助!!

回答by JaakL

If you have cable connection and Mac, then there is simple and powerful method:

如果您有电缆连接和 Mac,那么有一个简单而强大的方法:

  1. install free Wireshark, make sure that it can capture devices with (and you need to do this after every computer restart!):

    sudo chmod 644 /dev/bpf*

  2. Now share your network with wifi. System preferences > Sharing > Internet Sharing. Check that you have "Share your connections from: Ethernet" and using: Wi-Fi. You may want to also to configure some wifi security, it does not disturb your data monitoring.

  3. Connect your phone to your newly created network. I need quite often several attempts here. If the phone does not want to connect, turn of wifi of Mac, then repeat step 2 above and be patient.

  4. Start Wireshark capture your wireless interface with Wireshark, it is probably "en1". Filter your needed IP addresses and/or ports. When you find a package which is interesting, select it, Right-click (context menu) > Follow TCP Stream and you see nice text representation of the requests and answers.

  1. 安装免费的 Wireshark,确保它可以捕获设备(并且您需要在每次计算机重新启动后执行此操作!):

    须藤 chmod 644 /dev/bpf*

  2. 现在与 wifi 共享您的网络。系统偏好设置 > 共享 > Internet 共享。检查您是否有“从以下位置共享您的连接:以太网”并使用:Wi-Fi。您可能还想配置一些 wifi 安全性,它不会干扰您的数据监控。

  3. 将手机连接到新创建的网络。我经常需要在这里尝试几次。如果手机不想连接,关闭Mac的wifi,然后重复上面的第2步,耐心等待。

  4. 启动 Wireshark 用 Wireshark 捕获您的无线接口,它可能是“en1”。过滤您需要的 IP 地址和/或端口。当你发现一个有趣的包时,选择它,右键单击(上下文菜单)> Follow TCP Stream,你会看到请求和答案的漂亮文本表示。

And what is the best: exactly the same trick works for Android also!

什么是最好的:完全相同的技巧也适用于 Android!

回答by Konstantin Salavatov

回答by August Lin

  1. Install WireShark
  2. get ip address from xcode network monitor
  3. listen to wifi interface
  4. set filter ip.addr == 192.168.1.122 in WireShark
  1. 安装 WireShark
  2. 从 xcode 网络监视器获取 ip 地址
  3. 听wifi接口
  4. 在 WireShark 中设置过滤器 ip.addr == 192.168.1.122

回答by Simon Epskamp

A good solution if you are used to the chrome inspector tools is Pony debugger: https://github.com/square/PonyDebugger

如果您习惯了 chrome 检查器工具,一个很好的解决方案是 Pony 调试器:https: //github.com/square/PonyDebugger

It is a bit of a pain to setup, but once you do it work well. Be sure to use Safari instead of Chrome to use it though.

设置起来有点麻烦,但是一旦你做好了它就可以很好地工作。请务必使用 Safari 而不是 Chrome 来使用它。