iOS键盘拓展使用系统摄像头

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];
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值