Skip to content

Pankajbawane/NetworkSpectator

Repository files navigation

NetworkSpectator: Monitor/Intercept HTTP Traffic on iOS and macOS

Swift 6.0+ Platforms SPM Compatible License: MIT Build

A Swift framework for monitoring and inspecting your app's HTTP traffic during development and testing. NetworkSpectator captures requests and responses, provides a clean UI for browsing and mocking them, and allows you to export logs for sharing.

Features

  • Real-time network monitoring

    • Capture URL, method, status code, response time, headers, request body, and response body
    • Live updates with in-progress indicators for pending requests
    • Start immediately or use on-demand mode to enable monitoring from the UI when needed
    • Color-coded list view with method badges, status indicators, and response metrics
  • Filtering and search

    • Filter by status code ranges and HTTP methods
    • Combine multiple filters with visual filter chips
    • Full-text URL search across all captured requests
  • Detailed request inspection

    • Tabbed detail view: Overview, Request, Headers, and Response
    • Smart response rendering — pretty-printed JSON, inline image previews, and plain text
    • Copy any request or response data to clipboard
    • Create a mock or skip rule directly from a captured request
  • Export in multiple formats

    • CSV — bulk or single request export for spreadsheets and analysis
    • Plain text — human-readable format for quick sharing
    • Postman Collection — import directly into Postman for API testing
  • Mock responses

    • Intercept requests and return custom responses without a backend
    • Flexible matching: hostname, URL, path, endPath, subPath
    • Configure status codes, headers, JSON/raw body, and response delay
    • Programmatic mocking — register mocks via code for unit tests and development
    • UI-based mocking — let QA testers create and manage mocks on the fly without Xcode
    • Persist mocks across app sessions with local storage
  • Skip request logging

    • Exclude noisy or sensitive requests using the same flexible matching rules
    • Configure skip rules programmatically or from the UI
    • Persist rules across app launches
  • Insights dashboard

    • Summary cards: total requests, success rate, and unique hosts
    • Interactive charts for status code distribution, HTTP methods, host traffic, and request timeline
  • Log history

    • Automatically save session logs to disk for later review
    • Browse past sessions from Tools
    • Enable or disable history persistence from settings
  • Lightweight and easy to integrate

    • One-line setup to start monitoring
    • No XIB/Storyboards, no external dependencies
    • Works with SwiftUI, UIKit, and AppKit
    • Toggle debug console logging on or off
    • Supports both light and dark mode
  • Cross-platform

    • iOS 16.0+ / macOS 13.0+

Installation

Swift Package Manager

Add NetworkSpectator to your project using Swift Package Manager:

  1. In Xcode, select File > Add Package Dependencies...
  2. Enter the package repository URL - https://github.com/Pankajbawane/NetworkSpectator.git

Or add it to your Package.swift:

dependencies: [
    .package(url: "https://github.com/pankajbawane/NetworkSpectator.git", branch: "main")
]

Usage

Example App

The NetworkSpectatorExample app demonstrates basic usage of the library: https://github.com/Pankajbawane/NetworkSpectatorExample

Basic Setup

  1. Enable NetworkSpectator in your app's entry point (AppDelegate or App struct):

Call NetworkSpectator.start() to begin listening to HTTP requests. This will automatically log all HTTP traffic.

import NetworkSpectator

@main
struct MyApp: App {

    var body: some Scene {
        WindowGroup {
            ContentView()
                    .task {
                        #if DEBUG
                        NetworkSpectator.start()
                        #endif
                      }
        }
    }
}
  1. Present the NetworkSpectator UI:

SwiftUI

import NetworkSpectator

ContentView() {
}
  .sheet(isPresented: $showLogs) {
      NetworkSpectator.rootView
  }

UIKit (iOS)

import NetworkSpectator

let networkVC = NetworkSpectator.rootViewController
present(networkVC, animated: true)

AppKit (macOS)

import NetworkSpectator

let networkVC = NetworkSpectator.rootViewController
presentAsSheet(networkVC)

Configuration

Customize NetworkSpectator behavior with the configuration methods:

// Enable or disable printing logs to the debug console
NetworkSpectator.debugLogsPrint(isEnabled: Bool)

// Register a mock response
NetworkSpectator.registerMock(for mock: Mock)

// Remove all registered mocks
NetworkSpectator.stopMocking()

// Skip logging for specific requests
NetworkSpectator.ignoreLogging(for rule: MatchRule)

// Remove all skip logging rules
NetworkSpectator.stopIgnoringLog()

On-Demand Monitoring

Start NetworkSpectator in on-demand mode to let users enable monitoring from the UI:

NetworkSpectator.start(onDemand: true)

Disabling NetworkSpectator

If enabled, then, to stop network monitoring:

NetworkSpectator.stop()

NetworkSpectator on iOS

The following screenshots demonstrate NetworkSpectator running on iOS.

List of Requests Filters URL Search Details
landing filters_ios url_search_ios basic_ios
Headers Response Tools History
headers_ios response_response settings_ios share_ios
Insights Insights - Timeline Insights - Status code Insights - Performance
insights_ios timeline_ios status_code_ios perf_ios

NetworkSpectator on macOS

The following screenshots demonstrate NetworkSpectator running on macOS.

List of Requests Filters Details
landing_mac filters_mac basic_details_mac
Headers Response Tools
headers_mac response_mac analytics_mac
Insights Timeline Performance
settings_mac add_mock_mac skip_logging_mac

Safety and Release Builds

Because NetworkSpectator captures and displays network information, you should limit it to debug/test builds only. Wrap your integration points with #if DEBUG to ensure nothing leaks into release builds.

Recommendations:

  • Always guard with #if DEBUG and/or internal feature flags
  • Ensure NetworkSpectator is not initialized in release configurations

Example:

// Monitoring will start only for a debug build.
#if DEBUG
NetworkSpectator.start()
#endif

Requirements

  • Swift 6+
  • iOS 16.0+ / macOS 13.0+
  • Xcode 16.0+

LICENSE

MIT license. View LICENSE for more details.

About

A network monitoring and intercepting Swift library to observe and inspect network traffic during development and testing. It is an interceptor tool which captures requests and responses, lets browse them and export.

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Contributors

Languages