r/SwiftUI • u/Excellent_Sky_5838 • 28d ago
Question Is there any per-process network stat utility for macOS or native API for SwiftUI
I'm developing a native macOS app where I need to fetch per-process network (download/upload) stats as one of the requirement, but I couldn't find any native macOS API or any swift package to get this.. after some research, I couldn't find any from docs, I went with AI if it could do it... but at the end I couldn't find any native approach to do this.. for now I'm using nettop util as temporary fix but its very unreliable
3
Upvotes
3
u/Open_Shine135 28d ago
For normal app traffic, I'd investigate NEFilterDataProvider. NEFilterFlow exposes sourceProcessAuditToken for process attribution, and statisticsReportFrequency enables periodic .statistics reports. You'd need to ship an entitled Network Extension provider, so check the deployment requirements before committing to it.
Statistics reporting: https://developer.apple.com/documentation/networkextension/nefilternewflowverdict/statisticsreportfrequency
Deployment: https://developer.apple.com/documentation/technotes/tn3134-network-extension-provider-deployment
1
u/ChrononautPete 28d ago
https://www.devontechnologies.com/apps/freeware
1
u/sapplex1 27d ago
I hit a similar wall on a smaller scale. I wanted clean per-app numbers and kept finding tools that were close but not quite right for what I was building. I treated nettop as a quick sanity check only. The numbers were useful for spotting a big outlier, but I would not build a feature on them. The native route others mentioned sounds much more solid, but also much heavier to ship and maintain. That tradeoff is what stopped me. I ended up asking whether I really needed per-process detail in the first version, or just a reliable total with a rough breakdown. Is the per-process split a must-have for your first version, or more of a diagnostic you want while you develop it?
1
u/Funny_Cry2908 27d ago
Most of that CPU is probably the process launch plus the first sample, not the parsing. Keep one nettop alive and stream it:
nettop -P -d -L 0 -s 2 -x -J bytes_in,bytes_out-L 0 keeps logging forever, -d should give you per-interval deltas so you can skip the diffing, and you just read the pipe line by line with a FileHandle readabilityHandler. Restart it only if it exits.
The NEFilterDataProvider route works, but you're signing up for a system extension, a special entitlement and a user approval prompt, which is a lot for an admin tool. Is this going out through the App Store or direct?
1
u/[deleted] 27d ago
[removed] — view removed comment
0
u/[deleted] 27d ago
[removed] — view removed comment