0

I have a problem from 3 days :( I want to hook in CLLocationManagerDelegate protocol this method:

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations

I tried everything but without success. I know how to hook into class or framework but I can't find a solution to hook a Delegate. Please help me! Thanks

user3078845
  • 3
  • 1
  • 5

1 Answers1

4

Hooking requires you to provide objective-C class you would like to hook. This is what Class type is for. One way to get obj-c class is by name via objc_getClass function. But in your case as I understand it correctly you don't have the name. You want to hook every class that conforms to CLLocationManagerDelegate protocol and implements specific method. Here is what you can do.

You can obtain every registered obj-C class and search for those which conform toCLLocationManagerDelegate protocol like this:

static IMP original_didUpdateLocations;
void replaced_didUpdateLocations(id self, SEL _cmd, CLLocationManager* manager, NSArray* locations)
{
    NSLog(@"%@ did update locations to %@", manager, locations);

    original_didUpdateLocations(self, _cmd, manager, locations);
}

...

#import <objc/runtime.h>

int numClasses = objc_getClassList(NULL, 0);

Class* list = (Class*)malloc(sizeof(Class) * numClasses);
objc_getClassList(list, numClasses);    

for (int i = 0; i < numClasses; i++)
{
    if (class_conformsToProtocol(list[i], @protocol(CLLocationManagerDelegate)) && 
        class_getInstanceMethod(list[i], @selector(locationManager:didUpdateLocations:)))
    {
        MSHookMessageEx(list[i], @selector(locationManager:didUpdateLocations:), (IMP)replaced_didUpdateLocations, (IMP*)&original_didUpdateLocations);
    }
}

free(list);
  1. We need to know how many classes there is. objc_getClassList(NULL, 0) returns number of all registered classes.

  2. Allocating memory with malloc(sizeof(Class) * numClasses) and filling it with objects of type Class using objc_getClassList(list, numClasses).

  3. Searching through all these classes for those which conform to CLLocationManagerDelegate protocol and implement locationManager:didUpdateLocations: method. If we found one we are hooking it with our own implementation.

  4. In our own implementation we are printing some debug message and calling original implementation before returning. Of course, you can do whatever you what, this is just an example.

  5. Freeing allocated memory using free(list).

creker
  • 9,400
  • 1
  • 30
  • 47
  • Thanks for replay but I have a problem with this code. Usually I use logos in iOSOpenDev and I hook class in this way: `%hook ClassName -(void)Action { //Hooked launch } %end` how can I use your code? Or how can I translate your code in logos? Thanks again – user3078845 Dec 23 '13 at 09:16
  • I don't think you can use logos if you don't know exact class name you want to hook. My code requires you to use Mobile Substrate API manually. Code will not be very pretty but it's not that much more difficult to use than logos. – creker Dec 23 '13 at 12:12
  • I've updated my answer with complete example. Should be no problem getting it to compile and run. – creker Dec 23 '13 at 13:26
  • Thank you very much! You are very kind! But I'm not able to compile this code, I don't understand in which project I have to put it :( – user3078845 Dec 23 '13 at 20:24
  • I found a document ([link](http://www.google.it/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&ved=0CDsQFjAB&url=http%3A%2F%2Fwww.priyaontech.com%2Fdownload%2F12%2F&ei=Fqe4UsDUNqn1yAPSu4HYCw&usg=AFQjCNGilwd4UpBSNUMKkjRCy18S0dBc6A&sig2=1WjruioTXw82uzRQfYqLuA&bvm=bv.58187178,d.Yms) on page 30...) with example to translate code for logos but is very hard for me... Any help is appreciated! Please help me! – user3078845 Dec 23 '13 at 21:13
  • Just use that code in your tweak.xm as the common `c/c++` code. – Suge Jan 27 '15 at 11:41