swift-interception

main

Package for interception of objc selectors in Swift.
CaptureContext/swift-interception

swift-interception

SwiftPM 5.9 Platforms @capturecontext

Package for interception of objc selectors in Swift.

Macros: swift-interception-macros

Usage

Basic

Observe selectors on NSObject instances

import Interception

navigationController.setInterceptionHandler(
  for: _makeMethodSelector(
    selector: UINavigationController.popViewController,
    signature: UINavigationController.popViewController
  )
) { result in 
  print(result.args) // `animated` flag
  print(result.output) // popped `UIViewController?`
}

You can set up multiple interception handlers as well, just make sure that you use different keys for each handler

import Intercexption

object.setInterceptionHandler(
  for: _makeMethodSelector(
    selector: #selector(MyObject.someMethod(arg1:arg2)),
    signature: MyObject.someMethod(arg1:arg2)
  ),
  key: "argumentsPrinter"
) { result in 
  // In case of multiple arguments
  // you can access them as a tuple
  print(result.args.0)
  print(result.args.1)
}

Library development

If you use it to create a library it may be a good idea to export custom selectors implicitly

// Exports.swift
@_exported import _InterceptionCustomSelectors

Also you may find some @_spi methods and Utils helpful

@_spi(Internals) import Interception
import _InterceptionUtils // Is not shown in the autocomplete

Installation

Basic

You can add CombineInterception to an Xcode project by adding it as a package dependency.

  1. From the File menu, select Swift Packages › Add Package Dependency…
  2. Enter "https://github.com/capturecontext/swift-interception.git" into the package repository URL text field
  3. Choose products you need to link them to your project.

Recommended

If you use SwiftPM for your project, you can add CombineInterception to your package file.

.package(
  url: "https://github.com/capturecontext/swift-interception.git", 
  .upToNextMinor(from: "0.2.0")
)

Do not forget about target dependencies:

.product(
  name: "Interception", 
  package: "swift-interception"
)

License

This library is released under the MIT license. See LICENCE for details.

See ACKNOWLEDGMENTS for inspiration references and their licences.

Description

  • Swift Tools 5.9.0
View More Packages from this Author

Dependencies

  • None
Last updated: Thu Feb 29 2024 08:19:36 GMT-1000 (Hawaii-Aleutian Standard Time)