Search code examples
iosobjective-cjailbreaktheoslogos

Making a Theos hook into a particular category of a class


Normally when you are making a tweak and you want to hook into a application you do something like this:

%hook foo

//code    

%end

But now I have a @interface that has a weird name: @interface NSString (foo), and I have no idea how to hook into this. I tried this:

%hook NSString (foo)



%end

But this gives an error:

Tweak.xm:12:3: error: C++ requires a type specifier for all declarations
(foo)
~^Tweak.xm:12:18: error: expected ';' after top level declarator (foo):


Solution

  • @interface NSString (foo)
    

    Is simply declaring a custom category called "foo" that allows additional methods to be declared as part of the original NSString class. You could use standard logos hooking as if the methods that followed were of the NSString class. The one BIG exception though is that there are no instance variables of class categories. That means MSHookIvar would not work. Simple example tho to ur question- assuming orig code was:

    @interface NSString (foo)
    +(NSString *) someString;
    @end
    
    @implementation NSString (foo)
    +(NSString *) someString 
    { 
    //code that returns a string
    }
    @end
    

    To override the method, you could put:

    %hook NSString
    +(NSString *) someString {
    //code here
    return someNewString;
    }
    %end
    

    Note: Just typed this up on iPhone and have not compiled it, but it should work, or at least the concept should make more sense. Just google "customizing existing classes with obj c apple documentation" for more info on class categories.