Coder Social home page Coder Social logo

capturecontext / swift-interception Goto Github PK

View Code? Open in Web Editor NEW
0.0 0.0 0.0 56 KB

Package for interception of objc selectors in Swift.

Home Page: https://swiftpackageindex.com/CaptureContext/swift-interception/0.3.0/documentation/interception

License: MIT License

Makefile 3.66% Swift 94.94% Objective-C 1.40%
interception objc objc-runtime objective-c reflection runtime selector selectors swift swift-runtime

swift-interception's Introduction

swift-interception

CI SwiftPM 5.9 Platforms @capturecontext

Package for interception of objc selectors in Swift.

Usage

Basic

Observe selectors on NSObject instances

import Interception

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

You can also simplify creating method selector with InterceptionMacros if you are open for macros

import InterceptionMacros

navigationController.setInterceptionHandler(
  for: #methodSelector(UINavigationController.popViewController)
) { result in 
  print(result.args) // `animated` flag
  print(result.output) // popped `UIViewController?`
}

Macros require swift-syntax compilation, so it will affect cold compilation time

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)
}
import InterceptionMacros

object.setInterceptionHandler(
  for: #methodSelector(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

See combine-interception for usage example.

Installation

Basic

You can add Interception 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.3.0")
)

Do not forget about target dependencies:

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

License

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

See ACKNOWLEDGMENTS for inspiration references and their licences.

swift-interception's People

Contributors

maximkrouk avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.