yulingtianxia/BlockHook
FreeHook Objective-C blocks. A powerful AOP tool.
FreeFree tier
About yulingtianxia/BlockHook
BlockHook is an open-source Objective-C library that uses libffi to hook Objective-C blocks. It enables developers to intercept block invocations with four hook modes: before, instead, after, and dead (deallocation callback). BlockHook allows modifying return values and arguments, invoking the original implementation, and removing hooks at any time. It supports custom structs, provides block mangle names, and manages tokens self-reliantly. The library integrates via CocoaPods or Carthage and works on iOS, tvOS, and macOS.
Key Features
Four hook modes: Before, Instead, After, and Dead (deallocation)
Modify return values and arguments
Invoke original implementation
Remove hook at any time
Traverse all hook tokens
Self-managed tokens
Support for custom structs
Integrates via CocoaPods and Carthage
Supports iOS, tvOS, macOS
API design inspired by Aspects
Pros & Cons
Pros
- Simple API similar to Aspects, easy to adopt
- Supports all hook modes including deallocation callback
- Allows direct modification of return values and arguments
- Can be removed dynamically
- Works across Apple platforms (iOS, tvOS, macOS)
- Actively maintained with 850+ stars
Cons
- Requires libffi dependency
- Only applicable to Objective-C blocks, not Swift closures or other languages
- May have overhead due to libffi invocation
Best For
Aspect-oriented programming on block callbacksDebugging and logging block executionModifying block behavior without altering original codeTracing block lifecycle (creation to deallocation)Block interception in unit tests
FAQ
What is BlockHook?
BlockHook is an open-source library that lets you hook Objective-C blocks using libffi, enabling before, instead, after, and dead hook modes.
How do I use BlockHook?
Use the method `block_hookWithMode:usingBlock:` on a block to register a hook. The block passed receives a BHInvocation object to control behavior.
Can I remove a hook?
Yes, call the `remove` method on the BHToken returned when hooking.
Does BlockHook support CocoaPods?
Yes, BlockHook supports both CocoaPods and Carthage.