iOS键盘内部无法打开系统摄像头,可使用扩展APP到宿主APP进行识别文本或者扫码,然后把对应数据通过APP Group共享数据回传到使用键盘的第三方应用进行输入识别到的数据。
扩展跳转APP的方法
#import <dlfcn.h>
- (void)onClickScanButton{
UIResponder *responder = self;
while (responder) {
if ([responder isKindOfClass:[UIApplication class]]) {
NSLog(@"yes:%@", responder);
break;
}
responder = [responder nextResponder];
NSLog(@"responder:%@", responder);
}
NSString *bundleID = @"";
if (@available(iOS 16.0, *)){
bundleID = [self hostBundleIDWithInputVC:self];
} else {
bundleID = [self.parentViewController valueForKey:@"_hostBundleID"];
}
NSString *toUtf8= [[NSString stringWithFormat:@"XXX://keyboard.exten.used.camera?fromAPP=%@", bundleID] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:toUtf8];
[responder performSelector:@selector(openURL:) withObject:url];
//使用extensionContext进行跳转
[[self extensionContext] openURL:url completionHandler:^(BOOL success) {
NSLog(@"Success? %i", success);
}];
}
//获取使用键盘APP的BundleID
- (NSString *)hostBundleIDWithInputVC:(UIInputViewController *)vc {
NSString *bundleID = nil;
@try {
bundleID = [vc.parentViewController valueForKey:@"_hostBundleID"];
} @catch (NSException *exception) {
NSLog(@"hostBundleID Get Failure 1 %@", exception);
} @finally {
}
if(bundleID && ![@"" isEqualToString:bundleID] && ![bundleID isEqual:@"<null>"]) {
return bundleID;
}
// 云控禁止xpc取法
// xpc取法 逆向自xx输入法 主要解决iOS16上获取不到的问题
@try {
id hostPid = [vc.parentViewController valueForKey:@"_hostPID"];
if (hostPid) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
id serverIns = [NSClassFromString(@"PKService") performSelector:NSSelectorFromString(@"defaultService")];
id lities = [serverIns performSelector:NSSelectorFromString(@"personalities")];
id infos = [lities objectForKey:[NSBundle mainBundle].bundleIdentifier];
id info = [infos objectForKey:hostPid];
id con = [info performSelector:NSSelectorFromString(@"connection")];
id xpcCon = [con performSelector:NSSelectorFromString(@"_xpcConnection")];
#pragma clang diagnostic pop
const char * (* cfunc)(id) = dlsym(RTLD_DEFAULT, "xpc_connection_copy_bundle_id");
if (cfunc != nil && xpcCon != nil) {
const char *res = cfunc(xpcCon);
bundleID = [NSString stringWithUTF8String:res];
return bundleID;
}
}
} @catch (NSException *exception) {
NSLog(@"hostBundleID Get Failure 2 %@", exception);
} @finally {
}
return nil;
}
宿主APP回跳方法
#import <objc/runtime.h>
#import <objc/objc.h>
#define USER_DEFAULT_IDENTIFIER @"USER_DEFAULT_IDENTIFIER"
#define USED_ASCAMERA_APP_BUNDLE_ID @"USED_ASCAMERA_APP_BUNDLE_ID"
@interface UISystemNavigationAction : NSObject
@property(nonatomic, readonly, nonnull) NSArray<NSNumber*>* destinations;
-(BOOL)sendResponseForDestination:(NSUInteger)destination;
@end
- (BOOL)jumpBackToPreviousApp {
NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:USER_DEFAULT_IDENTIFIER];
if (@available(iOS 17.0, *)){
NSString *lastAppBundleId = [userDefaults valueForKey:USED_ASCAMERA_APP_BUNDLE_ID];
[userDefaults setValue:@"" forKey:USED_ASCAMERA_APP_BUNDLE_ID];
[userDefaults synchronize];
Class LSApplicationWorkspace = objc_getClass("LSApplicationWorkspace");
NSObject *workspace = [LSApplicationWorkspace performSelector:@selector(defaultWorkspace)];
if (!workspace) {
return NO;
}
BOOL isOpenApp = [workspace performSelector:@selector(openApplicationWithBundleID:) withObject:lastAppBundleId];
return isOpenApp;
} else {
Ivar sysNavIvar = class_getInstanceVariable(UIApplication.class, "_systemNavigationAction");
UIApplication *app = UIApplication.sharedApplication;
UISystemNavigationAction *action = object_getIvar(app, sysNavIvar);
if (!action) {
return NO;
}
NSUInteger destination = action.destinations.firstObject.unsignedIntegerValue;
return [action sendResponseForDestination:destination];
}
}