IOS
I do not think this is possible on iOS.
I did not find a public API for network monitoring / packet filtering. There is a possibility that such an API exists, but it is hidden. But in this case, Apple App Store Review Guides :
2.5 Applications that use non-public APIs will be rejected
If you need one specific quote to show that this is not possible, you can use this:
iOS does not support packet tracing directly. However, if you connect your iOS device to the Mac via USB ...
from the official Apple Technical Q & A QA1176 .
Alternatives
The next best thing is to manually configure the proxy server in the settings and then filter the traffic on the server side. Launching a proxy locally on the device is not an option due to the limitations of iOS background tasks:
2.16. Multitasking applications can only use background services for their intended purpose: VoIP, audio playback, location, task completion, local notifications, etc.
In addition, this post suggests that it might be possible to set up a VPN connection programmatically on iOS 8. It would also require sending device traffic, and I'm not sure if this method meets the recommendations.
Non-alternative
Some applications provide network traffic measurement functionality. But they use a dedicated API for network statistics: tracking usage / monitoring of iPhone usage .
There are also methods for batch tracing on iOS via the USB cable described here .
Android
In Android, you can configure the device to use your application as a VPN service. But:
- To do this, you need to display a dialog box describing the consequences of granting permission to work as a VPN.
- You need to show a constant notification while the VPN is active. An example application that does this is tPacketCapture .
To request user permission, you call VpnService.prepare
:
public void onClick(View v) { Intent intent = VpnService.prepare(getApplicationContext()); if (intent != null) { startActivityForResult(intent, 0); } else { onActivityResult(0, RESULT_OK, null); } }
and process the result, starting your VpnService.
protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { Intent intent = new Intent(this, MyVpnService.class); startService(intent); } }
Your VpnService
should implement public int onStartCommand()
. The service is considered as a foreground service and should not be killed by the OS.
This question: Android VpnService for packet capture will not capture packets , and it comments on shedding light on packet processing.