FMWebViewJavascriptBridge
FMWebViewJavascriptBridge copied to clipboard
不能继承interface
恩 目前是不支持,但是应该不会有那么深的接口层次吧
@carlSQ
一般会做基础封装, 特别的业务需要定制, 在保证保持JS的命名空间一致的情况下, 就需要继承去扩展了.
这是我修改后的NSObject+FMAnnotation.h代码, 用递归实现了继承.
#import "NSObject+FMAnnotation.h"
#import <objc/runtime.h>
@implementation NSObject (FMAnnotation)
- (NSDictionary *)methodAnnotations {
NSMutableDictionary *methodsMap = objc_getAssociatedObject(self, _cmd);
if (methodsMap) {
return methodsMap;
}
methodsMap = [self classExportedMethods:self.class];
objc_setAssociatedObject(self, _cmd, methodsMap, OBJC_ASSOCIATION_COPY_NONATOMIC);
return methodsMap;
}
- (NSMutableDictionary *)classExportedMethods:(Class)moduleClass {
NSMutableDictionary *methodsMap = [NSMutableDictionary dictionary];
unsigned int methodCount;
Method *methods = class_copyMethodList(object_getClass(moduleClass), &methodCount);
for (unsigned int i = 0; i < methodCount; i++) {
Method method = methods[i];
SEL selector = method_getName(method);
if ([NSStringFromSelector(selector) hasPrefix:@"__fm_export__"]) {
IMP imp = method_getImplementation(method);
NSDictionary *methodMap = ((NSDictionary *(*)(id, SEL)) imp)(moduleClass, selector);
NSAssert(methodMap.count, @"dont export method");
NSString *exportMethodName = methodMap.allKeys[0];
NSAssert(exportMethodName.length, @"dont export method name error");
NSRange range = [exportMethodName rangeOfString:@":"];
NSString *name = range.location != NSNotFound ? [exportMethodName substringToIndex:range.location] : exportMethodName;
[methodsMap setValue:methodMap[exportMethodName] forKey:name];
}
}
free(methods);
if (moduleClass.superclass != [NSObject class]) {
[methodsMap addEntriesFromDictionary:[self classExportedMethods:moduleClass.superclass]];
}
return methodsMap;
}
@end
根据你的思路写了一种惯用的实现方式:https://github.com/Ftkey/LTJSBridge 非常感谢你的开源代码。