V10(逻辑教育)

iOS-底层探索10:消息转发机制

2020-09-24  本文已影响0人  differ_iOSER

iOS 底层探索 文章汇总

目录

一、前言

上一篇文章iOS 方法的慢速查找流程分析中我们分析了方法的慢速查找流程,以及动态方法决议,那么这篇文章我们就一起分析消息的转发机制。

二、分析消息转发调用了哪些方法

@interface LGPerson : NSObject
//这里定义方法的声明主要是为了main中调用不报错;
//也可以这样调用[person performSelector:@selector(sayHello)];
//还可以设置Xcode不检查编译错误
- (void)sayHello;
@end

@implementation LGPerson
@end

调用:
LGPerson *person = [LGPerson alloc];
[person sayHello];

打印结果:
-[LGPerson sayHello]: unrecognized selector sent to instance 0x101241bb0

由于打印结果中看不到消息转发调用的方法,所以方法调用修改如下:

extern void instrumentObjcMessageSends(BOOL flag);

LGPerson *person = [LGPerson alloc];
instrumentObjcMessageSends(YES);
[person sayHello];
instrumentObjcMessageSends(NO);

运行后会把方法调用流程写到/tmp/msgSends-文件中

需要注意的是不能在源码中使用instrumentObjcMessageSends,在源码中使用不会将方法调用流程写进文件中


因此我们知道动态方法决议resolveInstanceMethod后调用了forwardingTargetForSelectormethodSignatureForSelector

三、实现消息转发方法

3.1 forwardingTargetForSelector

Apple Developer网站搜索forwardingTargetForSelector可发现:

该方法为一个实例方法、且需要返回一个处理方法的对象

实例代码:

@interface LGPerson : NSObject
- (void)sayHello;
@end

@implementation LGPerson
- (id)forwardingTargetForSelector:(SEL)aSelector {
    NSLog(@"%@",NSStringFromSelector(aSelector));
    return [LGStudent alloc];
}
@end
@interface LGStudent : NSObject
- (void)sayHello;
@end

@implementation LGStudent
- (void)sayHello {
    NSLog(@"%s",__func__);
}
@end

main方法中的调用如下:

LGPerson *person = [LGPerson alloc];
[person sayHello];

打印结果:
sayHello
-[LGStudent sayHello]

3.2 methodSignatureForSelector

官方文档如下:

该方法为一个实例方法、且必须调用forwardInvocation:方法并返回NSInvocation对象

实例代码:

@interface LGPerson : NSObject
- (void)sayHello;
@end

@implementation LGPerson
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
    NSLog(@"%s -- %@",__func__,NSStringFromSelector(aSelector));
    return [NSMethodSignature signatureWithObjCTypes:"v@:"];
}

// 不处理方法的实现
- (void)forwardInvocation:(NSInvocation *)anInvocation {
    NSLog(@"%s -- %@",__func__, anInvocation);
    NSLog(@"%@ -- %s",anInvocation.target, anInvocation.selector);
}
@end

方法签名参考TypeEncodingUrl
main方法中的调用如下:

LGPerson *person = [LGPerson alloc];
[person sayHello];

打印结果:
-[LGPerson methodSignatureForSelector:] -- sayHello

-[LGPerson forwardInvocation:] -- <NSInvocation: 0x10043c850>
<LGPerson: 0x100531070> -- sayHello

如果在forwardInvocation:中处理方法的实现:

- (void)forwardInvocation:(NSInvocation *)anInvocation {
    NSLog(@"%s - %@",__func__,anInvocation);
    NSLog(@"%@ - %s",anInvocation.target, anInvocation.selector);

    anInvocation.target = [LGStudent alloc];//LGStudent中实现了sayHello方法
    [anInvocation invoke];
}

打印结果:
- <NSInvocation: 0x100639650>
- sayHello

-[LGStudent sayHello]//这里调用了LGStudent中的sayHello方法

两个方法转发的区别:

3.3 iOS 消息的转发机制流程图如下:

消息转发机制

forwardingTargetForSelector也叫做快速转发方法
methodSignatureForSelector也叫做慢速转发方法

四、扩展--反汇编

前面使用了void instrumentObjcMessageSends(BOOL flag)方法打印出了消息转发时调用的方法,下面通过另外两个工具探索方法的调用流程

4.1 查看堆栈信息

点击左下角显示更全的堆栈信息:

点击可查看汇编代码调用:

bt打印堆栈信息:

4.2 查找镜像文件

现在我们需要通过CoreFoundation镜像文件反汇编还原CoreFoundation中的内部实现
image list读取所有加载的镜像文件:

搜索CoreFoundation找到其镜像路径:
/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation

4.3 Hopper反汇编工具的使用

CoreFoundation镜像文件拖到Hopper软件(Hopper是一个反汇编工具),保持默认选项即可。

Commend + F 全局搜索__forwarding_prep_0___

显示伪代码:

双击进入___forwarding___方法中

4.4 IDA_Pro_v7.0_Portable的介绍

由于Hopper不免费所以这里介绍另一个反汇编工具IDA,但是IDAMac版本存在闪退现象,所以这里使用Windows版本的。如果在Mac下使用需要先安装虚拟机(Parallels_Desktop、VMWare)然后装上Windows系统。

IDA

搜索___forwarding___

F5查看伪代码:





以上就是IDA中方法转发时的调用流程

上一篇 下一篇

猜你喜欢

热点阅读