方法swizzle在iOS 10.1上不起作用
我想在方法swizzle在iOS 10.1上不起作用,ios,objective-c,assembly,method-swizzling,Ios,Objective C,Assembly,Method Swizzling,我想在UIPasteboard中swizzle方法@property string: + (void) load { .... [UIPasteboard jr_swizzleMethod:@selector(string) withMethod:@selector(stringSwizzle) error:nil]; [UIPasteboard jr_swizzleMethod:@selector(setString:) withMethod:@selector(set
UIPasteboard
中swizzle方法@property string
:
+ (void) load {
....
[UIPasteboard jr_swizzleMethod:@selector(string) withMethod:@selector(stringSwizzle) error:nil];
[UIPasteboard jr_swizzleMethod:@selector(setString:) withMethod:@selector(setStringSwizzle:) error:nil];
....
}
当我调用[[UIPasteboard generalPasteboard]setString:@“test”]代码>输入我的swizzle方法。它适用于iOS 7、iOS 8和iOS 9。然而,它在iOS 10上失败了
组件与试验方法不同:
- (void) pasteboradTest {
[aUIPasteboard setString:@"test"];
}
在iOS 9上:
0xced0a <+0>: push {r7, lr}
0xced0c <+2>: mov r7, sp
0xced0e <+4>: sub sp, #0x8
0xced10 <+6>: movw r2, #0xa51c
0xced14 <+10>: movt r2, #0x0
0xced18 <+14>: add r2, pc
0xced1a <+16>: movw r3, #0xa2e2
0xced1e <+20>: movt r3, #0x0
0xced22 <+24>: add r3, pc
0xced24 <+26>: ldr r3, [r3]
0xced26 <+28>: movw r9, #0xbc8e
0xced2a <+32>: movt r9, #0x0
0xced2e <+36>: add r9, pc
0xced30 <+38>: movw r12, #0xc298
0xced34 <+42>: movt r12, #0x0
0xced38 <+46>: add r12, pc
0xced3a <+48>: str r0, [sp, #0x4]
0xced3c <+50>: str r1, [sp]
0xced3e <+52>: ldr.w r0, [r12]
0xced42 <+56>: ldr.w r1, [r9]
0xced46 <+60>: blx r3
0xced48 <+62>: add sp, #0x8
0xced4a <+64>: pop {r7, pc}
0xced0a:push{r7,lr}
0xced0c:mov r7,sp
0xced0e:子sp,#0x8
0xced10:movw r2,#0xa51c
0xced14:movt r2,#0x0
0xced18:添加r2,pc
0xced1a:movw r3,#0xa2e2
0xced1e:movt r3,#0x0
0xced22:添加r3,pc
0xced24:ldr r3,[r3]
0xced26:movw r9,#0xbc8e
0xced2a:movt r9,#0x0
0xced2e:添加r9,pc
0xced30:movw r12,#0xc298
0xced34:movt r12,#0x0
0xced38:添加r12,pc
0xced3a:STRR0[sp,#0x4]
0xced3c:str r1[sp]
0xced3e:ldr.w r0[r12]
0xced42:ldr.w r1[r9]
0xced46:blx r3
0xced48:添加sp,#0x8
0xced4a:pop{r7,pc}
在iOS 10上:
0x10008f78c <+0>: stp x29, x30, [sp, #-16]!
0x10008f790 <+4>: mov x29, sp
0x10008f794 <+8>: sub sp, sp, #16 ; =16
0x10008f798 <+12>: adrp x8, 9
0x10008f79c <+16>: add x8, x8, #592 ; =592
0x10008f7a0 <+20>: adrp x9, 11
0x10008f7a4 <+24>: add x9, x9, #3480 ; =3480
0x10008f7a8 <+28>: adrp x10, 12
0x10008f7ac <+32>: add x10, x10, #1776 ; =1776
0x10008f7b0 <+36>: str x0, [sp, #8]
0x10008f7b4 <+40>: str x1, [sp]
0x10008f7b8 <+44>: ldr x10, [x10]
0x10008f7bc <+48>: ldr x1, [x9]
0x10008f7c0 <+52>: mov x0, x10
0x10008f7c4 <+56>: mov x2, x8
0x10008f7c8 <+60>: bl 0x100094cb8 ; symbol stub for: objc_msgSend
0x10008f7cc <+64>: mov sp, x29
0x10008f7d0 <+68>: ldp x29, x30, [sp], #16
0x10008f7d4 <+72>: ret
0x10008f78c:stp x29,x30,[sp,#-16]!
0x10008f790:mov x29,sp
0x10008f794:子sp,sp,#16=16
0x10008f798:adrp x8,9
0x10008f79c:添加x8,x8,#592=592
0x10008f7a0:adrp x9,11
0x10008f7a4:添加x9、x9、#3480=3480
0x10008f7a8:adrp x10,12
0x10008f7ac:添加x10,x10,#1776=1776
0x10008f7b0:str x0[sp,#8]
0x10008f7b4:str x1[sp]
0x10008f7b8:ldr x10,[x10]
0x10008f7bc:ldr x1[x9]
0x10008f7c0:mov x0,x10
0x10008f7c4:mov x2,x8
0x10008f7c8:bl 0x100094cb8;用于:objc_msgSend的符号存根
0x10008f7cc:mov sp,x29
0x10008f7d0:ldp x29,x30,[sp],#16
0x10008f7d4:ret
UIPasteboard
成为iOS 10中的一个重要组件。注意这一点的最简单方法是在应用程序中的断点处停止,并在lldb中执行以下命令:
(lldb) po [UIPasteboard generalPasteboard]
<_UIConcretePasteboard: 0x6000000087a0>
当然,这不是为类集群实现swizzling的安全方法-不能保证您在运行时不会遇到任何其他私有的UIPasteboard
子类。sangforjr_swizzleMethod的代码是什么?那么您首先要做的是什么呢?它只是一个swizzle方法,就像那些blow:method\u交换实现(class\u getInstanceMethod([UIPasteboard class],@selector(setString:)),class\u getInstanceMethod([UIApplication class],@selector(StringSwizzle:))
,它将在[UIPasteboard generalPasteboard]
上工作,我只是在b[UIPasteboard setString::
b[UIPasteboard string]
上设置断点,运行代码[aUIPasteboard setString:@“test”]时它不会中断代码>和NSLog(@“%@,[aUIPasteboard string])
关于ldr
和ldr.w
之间的区别,请参见此答案:谢谢,但我认为这行可能是一个重要的地方0x10008f7c8:bl 0x100094cb8
,但我不太熟悉此行的功能
+ (void) load {
// ....
[[[UIPasteboard generalPasteboard] class] jr_swizzleMethod:@selector(string) withMethod:@selector(stringSwizzle) error:nil];
[[[UIPasteboard generalPasteboard] class] jr_swizzleMethod:@selector(setString:) withMethod:@selector(setStringSwizzle:) error:nil];
// ....
}