File tree Expand file tree Collapse file tree 2 files changed +42
-0
lines changed
Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ #import < UIKit/UIKit.h>
2+
3+ @interface UIApplication (Hook)
4+ @end
Original file line number Diff line number Diff line change 1+ #import " UIApplication+Hook.h"
2+ #import < objc/runtime.h>
3+
4+ @implementation UIApplication (Hook)
5+
6+ + (void )load {
7+ static dispatch_once_t onceToken;
8+ dispatch_once (&onceToken, ^{
9+ Class class = [self class ];
10+ SEL originalSelector = @selector (openURL: );
11+ SEL swizzledSelector = @selector (g_openURL: );
12+ Method originalMethod = class_getInstanceMethod (class, originalSelector);
13+ Method swizzledMethod = class_getInstanceMethod (class, swizzledSelector);
14+ if (originalMethod && swizzledMethod) {
15+ method_exchangeImplementations (originalMethod, swizzledMethod);
16+ NSLog (@" openURL: 方法交换成功!" );
17+ } else {
18+ NSLog (@" openURL: 方法交换失败!" );
19+ }
20+ });
21+ }
22+
23+ - (BOOL )g_openURL : (NSURL *)url {
24+ NSLog (@" 拦截到 openURL: %@ " , url);
25+ // 注意:由于方法交换,调用 g_openURL: 实际上会调用原 openURL: 实现
26+ // 这里我们调用新版 API
27+ if ([UIApplication.sharedApplication respondsToSelector: @selector (openURL:options:completionHandler: )]) {
28+ [UIApplication.sharedApplication openURL: url options: @{} completionHandler: ^(BOOL success) {
29+ NSLog (@" openURL:options:completionHandler: 返回 %d " , success);
30+ }];
31+ } else {
32+ // 如果低于 iOS 10,调用原始实现
33+ return [self g_openURL: url]; // 注意:由于方法交换,这里 g_openURL: 实际上是原 openURL:
34+ }
35+ return YES ;
36+ }
37+
38+ @end
You can’t perform that action at this time.
0 commit comments