Skip to content

Kofktu/Sniffer

Repository files navigation

Sniffer

Build Status Swift Platform CocoaPods Carthage compatible SwiftPM compatible

  • Automatic networking activity logger
  • intercepting any outgoing requests and incoming responses for debugging purposes.

alt tag

Requirements

  • iOS 8.0+, macOS 10.9+, watchOS 2.0+, tvOS 9.0+
  • Swift 5.0
  • Swift 4.2 (1.7.0)
  • Swift 4.0 (1.5.0)
  • Swift 3.0 (1.0.6)

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Installation

CocoaPods

Sniffer is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "Sniffer", '~> 2.0'

Carthage

For iOS 8+ projects with Carthage

github "Kofktu/Sniffer"

Usage

for any requests you make via 'URLSession'

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  Sniffer.register() // Register Sniffer to log all requests
  return true
}

for URLSessionConfiguration

let configuration = URLSessionConfiguration.default
Sniffer.enable(in: configuration)

for Custom Deserializer

public protocol BodyDeserializer {
    func deserialize(body: Data) -> String?
}

public final class CustomTextBodyDeserializer: BodyDeserializer {
    public func deserialize(body: Data) -> String? {
        // customization
        return String?
    }
}

Sniffer.register(deserializer: CustomTextBodyDeserializer(), for: ["text/plain"])

If you want to process the logs directly in your application

// Register the handler if you want the log to be handled directly by the application
Sniffer.onLogger = { (url, log) in
  print("\(url) : \(log)")
}

If you want to ignore domains

Sniffer.ignore(domains: ["github.com"])

References

Authors

Taeun Kim (kofktu), kofktu@gmail.com

License

Sniffer is available under the MIT license. See the LICENSE file for more info.