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

查看:17
本文介绍了挂钩 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

推荐答案

Hooking 需要你提供你想要 hook 的 Objective-C 类.这就是 Class 类型的用途.获取 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类并搜索符合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) 分配内存并使用 objc_getClassList(list, numClasses) 用 Class 类型的对象填充它.

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.

使用 free(list) 释放分配的内存.

Freeing allocated memory using free(list).

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

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