feat:invoke method find super class in iOS

This commit is contained in:
pengfei.zhou 2019-11-18 17:10:18 +08:00
parent d123baa5f4
commit 02bb3837b4

View File

@ -41,20 +41,34 @@ - (id)callNativeWithContextId:(NSString *)contextId module:(NSString *)module me
nativePlugin = [(DoricNativePlugin *) [pluginClass alloc] initWithContext:context]; nativePlugin = [(DoricNativePlugin *) [pluginClass alloc] initWithContext:context];
context.pluginInstanceMap[module] = nativePlugin; context.pluginInstanceMap[module] = nativePlugin;
} }
return [self findClass:pluginClass target:nativePlugin context:context method:method callbackId:callbackId argument:argument];
}
- (id)createParamWithMethodName:(NSString *)method context:(DoricContext *)context callbackId:(NSString *)callbackId argument:(id)argument {
if ([method isEqualToString:@"withPromise"]) {
return [[DoricPromise alloc] initWithContext:context callbackId:callbackId];
}
return argument;
}
- (id)findClass:(Class)clz target:(id)target context:(DoricContext *)context method:(NSString *)name callbackId:(NSString *)callbackId argument:(id)argument {
unsigned int count; unsigned int count;
id ret = nil; id ret = nil;
Method *methods = class_copyMethodList(pluginClass, &count); Method *methods = class_copyMethodList(clz, &count);
BOOL isFound = NO;
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
NSString *methodName = [NSString stringWithCString:sel_getName(method_getName(methods[i])) encoding:NSUTF8StringEncoding]; NSString *methodName = [NSString stringWithCString:sel_getName(method_getName(methods[i])) encoding:NSUTF8StringEncoding];
NSArray *array = [methodName componentsSeparatedByString:@":"]; NSArray *array = [methodName componentsSeparatedByString:@":"];
if (array && [array count] > 0) { if (array && [array count] > 0) {
if ([array[0] isEqualToString:method]) { if ([array[0] isEqualToString:name]) {
isFound = YES;
SEL selector = NSSelectorFromString(methodName); SEL selector = NSSelectorFromString(methodName);
NSMethodSignature *methodSignature = [nativePlugin methodSignatureForSelector:selector]; NSMethodSignature *methodSignature = [target methodSignatureForSelector:selector];
if (methodSignature) { if (methodSignature) {
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature]; NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
invocation.selector = selector; invocation.selector = selector;
invocation.target = nativePlugin; invocation.target = target;
__weak __typeof__(self) _self = self; __weak __typeof__(self) _self = self;
dispatch_block_t block = ^() { dispatch_block_t block = ^() {
__strong __typeof__(_self) self = _self; __strong __typeof__(_self) self = _self;
@ -94,14 +108,13 @@ - (id)callNativeWithContextId:(NSString *)contextId module:(NSString *)module me
if (methods) { if (methods) {
free(methods); free(methods);
} }
return ret; if (!isFound) {
} Class superclass = class_getSuperclass(clz);
if (superclass && superclass != [NSObject class]) {
- (id)createParamWithMethodName:(NSString *)method context:(DoricContext *)context callbackId:(NSString *)callbackId argument:(id)argument { return [self findClass:superclass target:target context:context method:name callbackId:callbackId argument:argument];
if ([method isEqualToString:@"withPromise"]) {
return [[DoricPromise alloc] initWithContext:context callbackId:callbackId];
} }
return argument; }
} return ret;
}
@end @end