Skip to content

A network monitoring and intercepting 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.

License

Notifications You must be signed in to change notification settings

Pankajbawane/NetworkSpectator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

88 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NetworkSpectator

A lightweight Swift utility 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

  • Monitor network requests and responses in real time

    • URL, method, status code, response time, request/response headers, request body and response body
    • Simple list and detail views for quick inspection
    • Analytics dashboard with charts for HTTP methods, status codes, and host distribution
  • Filters and Search

    • Filter requests by status codes and HTTP methods
    • Find specific requests by URL using search
  • Export logs in multiple formats

    NetworkSpectator supports multiple export formats:

    • CSV - For importing into spreadsheet applications or data analysis tools
    • Plain text - Simple format for quick sharing or viewing in text editors
    • Postman Collection format - Import directly into Postman for API testing and collaboration
  • Mock responses

    • Define custom mock responses using rule-based matching
    • Test different scenarios and edge cases without backend API deployment
    • Programmatic mocking - Add mocks via code without complex stubbing setups
    • UI-based mocking - Enable QA testers to validate business logic in test builds independently, without developer intervention or running app with Xcode
    • Helps to speed up development using mock response
    • Edit and update mock rules on the fly from UI
  • Skip a request from logging

    • Exclude specific or sensitive requests from logging using matching rules
    • Reduce noise by filtering out irrelevant requests
    • Configure skip rules both programmatically (in code) and dynamically (via UI)
    • Edit and update skip rules from UI
  • Lightweight and easy to integrate

    • One line setup to start monitoring and logging
    • No external dependencies
    • Works with SwiftUI and UIKit/AppKit
    • Manage log prints on Xcode debug console
  • Cross-platform support

    • 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)

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

Disabling NetworkSpectator

If enabled, then, to stop network monitoring:

NetworkSpectator.stop()

NetworkSpectator on iOS

The following screenshots demonstrate NetworkSpectator running on iOS in light mode.

List of Requests Filters URL Search Details
landing filters_ios url_search_ios basic_ios
Headers Response Settings Export & Share
headers_ios response_response settings_ios share_ios

NetworkSpectator on macOS

The following screenshots demonstrate NetworkSpectator running on macOS in dark mode.

List of Requests Filters Details
landing_mac filters_mac basic_details_mac
Headers Response Analytics
headers_mac response_mac analytics_mac
Settings Add Mock Skip Logging
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 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

Stars

Watchers

Forks

Releases

No releases published

Languages