#hook #macro #class #instance #objective-c #function #fn

oc-hook-macros

Some convenient macros for hooking Objective-C functions

2 releases

new 0.1.1 Dec 14, 2024
0.1.0 Dec 14, 2024

#700 in Rust patterns

Download history 202/week @ 2024-12-09

202 downloads per month

MIT license

13KB
324 lines

Usage

use oc_hook_macros::*;

hook_helper! {
    // hook instance method
    - (void) [NSApplication run]
    unsafe extern "C" fn hook_run(_this: &NSObject, _cmd: Sel) {
        log::error!("hook_run, bye bye");
    }

    // hook class method
    +(id)[ClassA hello]
    unsafe extern "C" fn hello(this: &NSObject, _cmd: Sel) -> *mut NSObject{
        // call the original method by the hooked selector which starts with `hook_`
        let ret: Retained<NSObject> msg_send_id![this, hook_hello];
        Retained::into_raw(ret)
    }
}

// insert new method into existing class
new_selector! {
    -(void)[NSObject openNewXXInstace:]
    unsafe extern "C" fn openNewXXInstace(_this:&NSObject, _cmd: Sel, _sender: *mut NSObject) {
        // ...
    }
}

Dependencies

~1–1.5MB
~30K SLoC