钩在CLLocationManagerDelegate协议 [英] Hook in CLLocationManagerDelegate protocol

查看:520
本文介绍了钩在CLLocationManagerDelegate协议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3天的问题:(我想钩住CLLocationManagerDelegate协议这个方法:

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

推荐答案

挂钩需要你提供你想挂钩的Objective-C类,这就是 class type。获取obj-c类的一种方法是通过 objc_getClass 函数的名称,但是在你的情况下,正确的理解,你没有名字,你想钩每一个符合 CLLocationManagerDelegate 协议的类,并实现特定的方法,这是您可以做的。

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.

您可以获得每个注册obj-C类,并搜索那些confor m到 CLLocationManagerDelegate 协议如下:

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. 我们需要知道有多少类。 objc_getClassList(NULL,0)返回所有注册类的数字。

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

使用 malloc(sizeof(Class)* numClasses)分配内存并填充类型的对象 class using objc_getClassList(list,numClasses)

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

搜索符合 CLLocationManagerDelegate 协议的所有这些类,并实现 locationManager:didUpdateLocations:方法。如果我们发现一个我们正在用自己的实现挂钩。

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.

在我们自己的实现中,我们打印一些调试消息,并在返回之前调用原始实现。当然,你可以做任何事情,这只是一个例子。

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.

使用免费(列表) code>。

Freeing allocated memory using free(list).

这篇关于钩在CLLocationManagerDelegate协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆