Skip to content

Mock iOS http API calls, without proxy and certificates, uses swizzling

License

Notifications You must be signed in to change notification settings

chanonly123/local-response

Repository files navigation

Local response

Local response is a powerful developer tool for iOS developers, allowing you to intercept and mock network traffic in your iOS apps without the need for certificates or proxy configurations. This tool is a lightweight alternative to Proxyman and Charles Proxy, specifically designed for simplicity and ease of use.

Features

  • Method Swizzling: Intercepts URLSession classes through method swizzling, capturing network requests and responses. No extra code changes required.
  • MacOS Companion App: Records all intercepted traffic in a dedicated macOS app, which listens for incoming data via a simple server.
  • Mock Responses: Easily override and mock network responses directly from the macOS app, enabling you to test various scenarios without modifying your code.
  • No Certificates or Proxy Needed: Unlike other tools, this solution does not require the installation of certificates or the use of a proxy, simplifying the setup process.

How to mock response

TODO

  • Recording requests directly to Map Local window
  • Adding unit tests

Installation

iOS Library

  1. Add the library to your project:

    • Using Swift Package Manager:

      dependencies: [
        .package(url: "https://github.com/chanonly123/local-response.git", from: "1.0.0")
      ]
    • Using Cocoapods: Add pod 'LocalResponse' to Podfile

    • Or by manually integrating the library into your project.

  2. Initialize the Interceptor:

In your AppDelegate or at the start of your app:

import LocalResponse

class AppDelegate: NSObject, UIApplicationDelegate {
   func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
     #if DEBUG
         // When using the iOS simulator in the same network
         LocalResponse.shared.connect()

         // When using other devices in the same network, pass the local url (availabe in the listener macos app)
         LocalResponse.shared.connect(connectionUrl: "http://192.168.0.108:4040")
     #endif
   }
}

macOS App

  1. Download the macOS app:

  2. Run the macOS app:

    • Launch the app, and it will automatically start listening for traffic from your iOS app.
    • Allow it to run from security options from settings.

Usage

  • Intercepting Traffic:

    • Once the library is initialized in your iOS app, all network requests made via URLSession will be intercepted and logged in the macOS app.
  • Mocking Responses:

    • In the macOS app, you can select any intercepted request and provide an overridden response. The iOS app will receive this mocked response as if it were from the actual server.

Examples

  • Logging Requests:

    • The macOS app provides a clear and detailed log of all intercepted network traffic, including URLs, headers, and body content.
  • Testing Error Scenarios:

    • Override network responses to simulate server errors, or custom data responses.

Screenshots

Allow the app to run

alt tag

Records all http network calls, You can edit (Response, Headers, StatusCode)

alt tag

Contributing

Contributions are most welcome! Please open an issue or submit a pull request with your improvements.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

Inspired by the need for simpler traffic interception without the hassle of certificate management. Apart from that most of the solutions are paid.

Thanks To

A big thanks to the following open-source projects and contributors whose code has been used in this project:

atlantis - Capture HTTP/HTTPS, and Websocket from iOS app without proxy.

FlyingFox - Lightweight, HTTP server written in Swift using async/await.

Factory - A new approach to Container-Based Dependency Injection for Swift and SwiftUI.

realm-swift - Realm is a mobile database: a replacement for Core Data & SQLite.

Highlightr - iOS & OSX Syntax Highlighter.

CodeEditor - A SwiftUI TextEditor with syntax highlighting using Highlight.js