.h文件
@interface CDVCommandQueue (CDVCommandQueueHook)
+ (void)hookUIApplication;
@end
.m文件
#import "CDVCommandQueue+CDVCommandQueueHook.h"
#import <objc/runtime.h>
#import "AppDelegate.h"
@implementation CDVCommandQueue (CDVCommandQueueHook)
+ (void)hookUIApplication
{
Method controlMethod = class_getInstanceMethod([CDVCommandQueue class], @selector(execute:));
Method hookMethod = class_getInstanceMethod([self class], @selector(hook_execute:));
method_exchangeImplementations(controlMethod, hookMethod);
}
// 调用插件后的处理
- (BOOL)hook_execute:(CDVInvokedUrlCommand*)command
{
// 操作历史存在内存中,最多只记录10条,如果闪退的话把最后10条调用记录和错误信息一块儿上报后台
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
if (appDelegate.pluginCallHistory.count >= 10) {
[appDelegate.pluginCallHistory removeObjectAtIndex:0];
}
NSString *str = [NSString stringWithFormat:@"className: %@, methodName: %@\n",command.className ? command.className : @"", command.methodName ? command.methodName : @""];
[appDelegate.pluginCallHistory addObject:str];
return [self hook_execute:command];
}
@end
在appDelegate引入文件 #import "CDVCommandQueue+CDVCommandQueueHook.h"
在- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions方法中增加以下代码
[CDVCommandQueue hookUIApplication];
这篇博客介绍了如何通过Objective-C的Category和Method Swizzling技术,在iOS应用中hook Cordova的CDVCommandQueue,以捕获并记录Cordova插件的调用历史。具体实现包括创建CDVCommandQueue的类别扩展,交换原始执行方法与hook方法,以及在AppDelegate中启动hook。记录的调用历史存储在内存中,最多保留10条,以便在必要时上报后台。
6万+

被折叠的 条评论
为什么被折叠?



