处理上架的问题
1:处理了openurl 拉起问题 2:去掉了http 3 隐私等等
This commit is contained in:
@@ -4,15 +4,86 @@
|
||||
//
|
||||
|
||||
#import "KBExtensionAppLauncher.h"
|
||||
|
||||
#if KB_URL_BRIDGE_ENABLE
|
||||
#import <objc/message.h>
|
||||
#endif
|
||||
|
||||
@implementation KBExtensionAppLauncher
|
||||
|
||||
#if KB_URL_BRIDGE_ENABLE
|
||||
+ (BOOL)kb_openURLViaResponderChain:(NSURL *)url
|
||||
source:(nullable UIResponder *)source
|
||||
completion:(void (^ _Nullable)(BOOL success))completion {
|
||||
if (!url) {
|
||||
if (completion) { completion(NO); }
|
||||
return NO;
|
||||
}
|
||||
|
||||
UIResponder *responder = source;
|
||||
|
||||
// 优先尝试 openURL:options:completionHandler:
|
||||
// 注意:在键盘扩展里走“响应链兜底”本身就存在不确定性;不同系统/宿主 App 的实现
|
||||
// 可能对 options 参数的类型有不同假设。为避免类型不匹配导致崩溃,options 统一传 nil。
|
||||
SEL openURLOptionsSel = NSSelectorFromString(@"openURL:options:completionHandler:");
|
||||
while (responder) {
|
||||
if ([responder respondsToSelector:openURLOptionsSel]) {
|
||||
void (*msgSend)(id, SEL, NSURL *, id, void (^)(BOOL)) = (void *)objc_msgSend;
|
||||
msgSend(responder, openURLOptionsSel, url, nil, ^(BOOL ok) {
|
||||
if (completion) { completion(ok); }
|
||||
});
|
||||
return YES;
|
||||
}
|
||||
responder = responder.nextResponder;
|
||||
}
|
||||
|
||||
// 尝试 openURL:completionHandler:
|
||||
responder = source;
|
||||
SEL openURLCompletionSel = NSSelectorFromString(@"openURL:completionHandler:");
|
||||
while (responder) {
|
||||
if ([responder respondsToSelector:openURLCompletionSel]) {
|
||||
void (*msgSend)(id, SEL, NSURL *, void (^)(BOOL)) = (void *)objc_msgSend;
|
||||
msgSend(responder, openURLCompletionSel, url, ^(BOOL ok) {
|
||||
if (completion) { completion(ok); }
|
||||
});
|
||||
return YES;
|
||||
}
|
||||
responder = responder.nextResponder;
|
||||
}
|
||||
|
||||
// 兜底:openURL:
|
||||
responder = source;
|
||||
SEL openURLSel = NSSelectorFromString(@"openURL:");
|
||||
while (responder) {
|
||||
if ([responder respondsToSelector:openURLSel]) {
|
||||
BOOL (*msgSend)(id, SEL, NSURL *) = (void *)objc_msgSend;
|
||||
BOOL ok = msgSend(responder, openURLSel, url);
|
||||
if (completion) { completion(ok); }
|
||||
return YES;
|
||||
}
|
||||
responder = responder.nextResponder;
|
||||
}
|
||||
|
||||
if (completion) { completion(NO); }
|
||||
return NO;
|
||||
}
|
||||
#endif
|
||||
|
||||
+ (void)openPrimaryURL:(NSURL * _Nullable)primaryURL
|
||||
fallbackURL:(NSURL * _Nullable)fallbackURL
|
||||
usingInputController:(UIInputViewController *)ivc
|
||||
source:(UIResponder *)source
|
||||
source:(UIResponder * _Nullable)source
|
||||
completion:(void (^ _Nullable)(BOOL success))completion {
|
||||
if (![NSThread isMainThread]) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
[self openPrimaryURL:primaryURL
|
||||
fallbackURL:fallbackURL
|
||||
usingInputController:ivc
|
||||
source:source
|
||||
completion:completion];
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (!ivc || (!primaryURL && !fallbackURL)) {
|
||||
if (completion) { completion(NO); }
|
||||
return;
|
||||
@@ -48,19 +119,38 @@
|
||||
finish(YES);
|
||||
return;
|
||||
}
|
||||
BOOL bridged = [self p_bridgeFirst:first second:second from:source];
|
||||
finish(bridged);
|
||||
|
||||
#if KB_URL_BRIDGE_ENABLE
|
||||
// 注意:上架有风险。仅当确实存在“宿主 App 拦截 extensionContext openURL”
|
||||
// 的场景且业务强依赖时才开启此兜底。
|
||||
UIResponder *start = (source ?: (UIResponder *)ivc.view ?: (UIResponder *)ivc);
|
||||
[self kb_openURLViaResponderChain:second
|
||||
source:start
|
||||
completion:^(BOOL ok3) {
|
||||
finish(ok3);
|
||||
}];
|
||||
#else
|
||||
finish(NO);
|
||||
#endif
|
||||
}];
|
||||
} else {
|
||||
BOOL bridged = [self p_bridgeFirst:first second:nil from:source];
|
||||
finish(bridged);
|
||||
#if KB_URL_BRIDGE_ENABLE
|
||||
UIResponder *start = (source ?: (UIResponder *)ivc.view ?: (UIResponder *)ivc);
|
||||
[self kb_openURLViaResponderChain:first
|
||||
source:start
|
||||
completion:^(BOOL ok3) {
|
||||
finish(ok3);
|
||||
}];
|
||||
#else
|
||||
finish(NO);
|
||||
#endif
|
||||
}
|
||||
}];
|
||||
}
|
||||
|
||||
+ (void)openScheme:(NSURL *)scheme
|
||||
usingInputController:(UIInputViewController *)ivc
|
||||
source:(UIResponder *)source
|
||||
source:(UIResponder * _Nullable)source
|
||||
completion:(void (^ _Nullable)(BOOL success))completion {
|
||||
[self openPrimaryURL:scheme
|
||||
fallbackURL:nil
|
||||
@@ -69,53 +159,4 @@
|
||||
completion:completion];
|
||||
}
|
||||
|
||||
#pragma mark - Private
|
||||
|
||||
// 通过响应链尝试调用 openURL:(等价于原 KBURLOpenBridge 实现)
|
||||
+ (BOOL)p_openURLViaResponder:(NSURL *)url from:(UIResponder *)start {
|
||||
#if KB_URL_BRIDGE_ENABLE
|
||||
if (!url || !start) return NO;
|
||||
SEL sel = NSSelectorFromString(@"openURL:");
|
||||
UIResponder *responder = start;
|
||||
while (responder) {
|
||||
@try {
|
||||
if ([responder respondsToSelector:sel]) {
|
||||
BOOL handled = NO;
|
||||
BOOL (*funcBool)(id, SEL, NSURL *) = (BOOL (*)(id, SEL, NSURL *))objc_msgSend;
|
||||
if (funcBool) {
|
||||
handled = funcBool(responder, sel, url);
|
||||
} else {
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
|
||||
[responder performSelector:sel withObject:url];
|
||||
handled = YES;
|
||||
#pragma clang diagnostic pop
|
||||
}
|
||||
return handled;
|
||||
}
|
||||
} @catch (__unused NSException *e) {
|
||||
// ignore and continue
|
||||
}
|
||||
responder = responder.nextResponder;
|
||||
}
|
||||
return NO;
|
||||
#else
|
||||
(void)url; (void)start;
|
||||
return NO;
|
||||
#endif
|
||||
}
|
||||
|
||||
+ (BOOL)p_bridgeFirst:(NSURL * _Nullable)first
|
||||
second:(NSURL * _Nullable)second
|
||||
from:(UIResponder *)source {
|
||||
BOOL bridged = NO;
|
||||
if (first) {
|
||||
bridged = [self p_openURLViaResponder:first from:source];
|
||||
}
|
||||
if (!bridged && second) {
|
||||
bridged = [self p_openURLViaResponder:second from:source];
|
||||
}
|
||||
return bridged;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user