Skip to content

Commit 9358efe

Browse files
zhuuvzhi365g1s22
andauthored
修复iOS18无法唤起QQ的bug (#125)
原理参考:https://blog.csdn.net/u014311747/article/details/144347339 Fix #121 --------- Co-authored-by: Alex <alex@gmail.com>
1 parent 28dc636 commit 9358efe

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

ios/Classes/UIApplication+Hook.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#import <UIKit/UIKit.h>
2+
3+
@interface UIApplication (Hook)
4+
@end

ios/Classes/UIApplication+Hook.m

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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

0 commit comments

Comments
 (0)