feat(ios): implement URLProtocol-based network interceptor#22
Merged
Conversation
Brings iOS to full parity with Android. Native layer (6 new files): - NetworkToolsInterceptor: NSURLProtocol subclass that intercepts all HTTP/HTTPS traffic through URLSession (the transport React Native uses on iOS). Marks forwarded requests with a property key to prevent infinite recursion. Caps response body capture at 256 KB; skips binary MIME types entirely. - NetworkToolsStorage: thread-safe NSLock-backed singleton, max 100 requests (mirrors NetworkRequestStorage.kt). - NetworkToolsManager: singleton that owns +activate (registers the URLProtocol, #if DEBUG only) and event emission via the module's RCTEventEmitter reference. Module bridge (updated): - NetworkTools now extends RCTEventEmitter so "NetworkTools:onRequest" events flow through the standard DeviceEventEmitter on both old-arch (bridge) and new-arch (JSI/TurboModule). getTurboModule: retained for new arch. - NetworkTools.podspec exposes NetworkToolsManager.h as a public header so Swift AppDelegates can call NetworkToolsManager.activate() after import NetworkTools. Expo plugin (iOS side added): - applySwiftAppDelegatePatch: adds import NetworkTools and inserts a #if DEBUG NetworkToolsManager.activate() #endif block at the top of application(_:didFinishLaunchingWithOptions:). - applyObjcAppDelegatePatch: same for Objective-C AppDelegate.mm. - Android snippets also updated to include the BuildConfig.DEBUG guard. - Plugin tests expanded from 8 to 18; all 41 suite tests pass. Example app: - AppDelegate.swift updated with the #if DEBUG activation block. README: iOS marked as Supported; setup covers both platforms. Closes #13 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ation - Added swizzling of NSURLSessionConfiguration - Improved request body handling by buffering HTTP body streams and capturing request data. - Updated NetworkToolsManager to use RCTEventEmitter for event emission. - Enhanced UI components to display request and response bodies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Brings iOS to full parity with Android. Every
fetch/XMLHttpRequest/axioscall made through React Native's networking layer is now captured on iOS, stored, and emitted as aNetworkTools:onRequestevent — exactly matching the Android behaviour.New files
ios/NetworkToolsInterceptor.{h,m}NSURLProtocolsubclass — intercepts all HTTP/HTTPSURLSessiontraffic; prevents infinite recursion via a request property key; caps body capture at 256 KB; skips binary MIME typesios/NetworkToolsStorage.{h,m}NSLock-backed singleton, max 100 requests (mirrorsNetworkRequestStorage.kt)ios/NetworkToolsManager.{h,m}+activateregisters theURLProtocol(#if DEBUGonly); holds a weakRCTEventEmitterreference for JS event emissionUpdated files
ios/NetworkTools.h/.mm—NetworkToolsnow extendsRCTEventEmitter(in addition to implementingNativeNetworkToolsSpec) soNetworkTools:onRequestevents flow through the standardDeviceEventEmitteron both old-arch (bridge) and new-arch (TurboModule/JSI).getTurboModule:retained for new arch.NetworkTools.podspec—NetworkToolsManager.his now a public header so SwiftAppDelegates can callNetworkToolsManager.activate()afterimport NetworkTools.Expo plugin —
applySwiftAppDelegatePatchandapplyObjcAppDelegatePatchadded.withNetworkToolsnow patches bothMainApplication(Android) andAppDelegate(iOS) in a singleexpo prebuildrun. Android snippets also updated to include theBuildConfig.DEBUGguard (forward-ports #14/#15).example/ios/.../AppDelegate.swift—#if DEBUG NetworkToolsManager.activate() #endifadded.README.md— iOS marked ✅ Supported; setup instructions cover both platforms.Architecture
Test plan
yarn jest)pod installinexample/iossucceeds with the new source filesyarn ioson simulator — requests from the example app appear in the floating monitorxcodebuild -configuration Release) —NetworkToolsManager.activate()is not called (#if DEBUGcompiled out), URLProtocol not registeredexpo prebuildon a fresh Expo iOS project →AppDelegate.swiftcontainsimport NetworkToolsand the#if DEBUGactivation blockCloses #13