项目中fishhook的一次实用
fishhook下载地址
main.m
#import "fishhook.h"
#import <dlfcn.h>
#pragma mark 根据环境选择plist与der文件
// 自定义的close函数
static int (*orig_close)(int);
// 定义函数的指针变量,用户保存原来的函数指针(函数其实也是一个地址)
static int (*orig_open)(const char *, int, ...);
// 自定义的close函数
int my_close(int fd) {
// printf("Calling real close(%d)\n", fd);
return orig_close(fd);
}
// 自定义的open函数
int my_open(const char *path, int oflag, ...) {
va_list ap = {0};
mode_t mode = 0;
if ((oflag & O_CREAT) != 0) {
// mode only applies to O_CREAT
va_start(ap, oflag);
mode = va_arg(ap, int);
va_end(ap);
// printf("Calling real open('%s', %d, %d)\n", path, oflag, mode);
return orig_open(path, oflag, mode);
} else {
// printf("Calling real open('%s', %d)\n", path, oflag);
NSString *pathOri = [[NSString alloc]initWithUTF8String:path];
/** 获取plist的时候*/
if ([pathOri rangeOfString:@"应用名.app/文件名.plist"].length > 0) {
NSLog(@"\n打开的路径====plist========\n%@",pathOri);
// 环境1
if ([@"请求路径"isEqualToString:@"https://环境1.com"]) {
path = [[pathOri stringByReplacingOccurrencesOfString:@"文件名.plist" withString:@"文件名_环境1.plist"] UTF8String];
}else if ([@"请求路径" isEqualToString:@"http://环境2.com"]){
// 环境2
path = [[pathOri stringByReplacingOccurrencesOfString:@"文件名.plist" withString:@"文件名_环境2.plist"] UTF8String];
}else{
// 生产
path = [[pathOri stringByReplacingOccurrencesOfString:@"文件名.plist" withString:@"文件名_生产.plist"] UTF8String];
}
NSLog(@"\n返回的路径====plist========\n%s",path);
}
return orig_open(path, oflag, mode);
}
}
#pragma mark plist方法调用
void modifyThePlistFile(int argc, char * argv[]){
struct rebinding binds[2];
// orig_close是一个函数指针,(void *)&orig_close 是一个返回参数,所以用取地址,(void *)&orig_open也是类似的
struct rebinding bind1 = {"close", my_close, (void *)&orig_close};
binds[0] = bind1;
binds[1] = (struct rebinding){"open", my_open, (void *)&orig_open};
rebind_symbols(binds, 2);
// Open our own binary and print out first 4 bytes (which is the same
// for all Mach-O binaries on a given architecture)
int fd = open(argv[0], O_RDONLY);
uint32_t magic_number = 0;
read(fd, &magic_number, 4);
printf("Mach-O Magic Number: %x \n", magic_number);
close(fd);
}
int main(int argc, char * argv[]) {
@autoreleasepool {
// 修改pilist
modifyThePlistFile(argc, argv);
return UIApplicationMain(argc, argv, nil, @"AppDelegate");
}
}
// 获取plist文件 方法1
NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"文件名" ofType:@"plist"];
// 获取plist文件 方法2
NSURL *URL=[[NSBundle mainBundle]URLForResource:@"文件名" withExtension:@"plist"];