美文网首页
记一次runtime尝试

记一次runtime尝试

作者: LD_X | 来源:发表于2017-10-13 20:32 被阅读16次

尝试:runtime取到类方法,遍历调用类方法,
结论:如果方法参数没有问题的话,是可以的。由于后期改变,最后参数没有测。
http://www.jianshu.com/p/47375bd248a8
还需注意参数匹配

背景:
1.APP原来支持门店角色登录,需求代理商也能使用。
2.APP功能描述:
门店可以向上进货,向下销货,同时具备营销(针对会员),其他增值功能(商学院(视频IM)已移除,分销,收款等)
代理商同时可以向上进货,向下销货,同时具备营销(针对门店)
so 业务整体相似
共用页面,共用API?猜测https域名贵,so不考虑域名不同。
前期沟通可能由于沟通不充分的原因,我这边尝试了三种方法。

最笨的方法,一开始沟通时,后台未知接口统计,说没有规律。
好,没规则,那我整理吧
项目原来是网络请求是从第一版的SDK移出来的,类方法请求。
可以使用runtime,遍历调用,保存接口到plist文件中作映射?

   unsigned int outCount = 0;
    Method *methodList = class_copyMethodList(object_getClass([CMBXXX class]) , &outCount);
    for (int i = 0; i < outCount; i++) {
        Method method = methodList[i];
        SEL selName = method_getName(method);
        [CMBXXX performSelector:<#(SEL)#>];
    }

可以由于参数的缘故最后尝试失败(以前sdk的坑,后期也没注意,参数其中有block参数,网络请求基本有回调,所以就没有判断这个block存在与否)

// 使用NSInvocation
SEL aSelector = NSSelectorFromString(@"doSoming:argument2:");
    NSInteger argument1 = 10;
    NSString *argument2 = @"argument2";
    if([self respondsToSelector:aSelector]) {
        NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:aSelector]];
        [inv setSelector:aSelector];
        [inv setTarget:self];
        [inv setArgument:&(argument1) atIndex:2];
        [inv setArgument:&(argument2) atIndex:3];
        [inv performSelector:@selector(invoke) withObject:nil afterDelay:15.0];
    }

上述方法也许可以实现填坑,不过仔细想想不对,我整理那么多,后台也需要整理那么多修改,真的没规则?不可能!

逼着后台,给我一个规则,老大的要求是原来API加前缀(即规则修改),写了一个接口用于根据原API修改返回最新的API,接口内部需要后台提供规则,最后几百个接口,目录有不同,后台无法提供,最后还是不了了只。

最后后台说API接口基本相同,部分可能映射
用户管理类(类似单例,注意不要gcd的once)

static NSString *userProfile = @"userProfile";
static UserProfile *instance = nil;
+ (instancetype)shareInstance {
    @synchronized(self) {
        if (instance == nil) {
            instance = [[UserProfile alloc] initWithSqlite];
        }
    }
    return instance;
}

附带一个字典属性及角色枚举,映射字典从plist读取,字典添加使用分类添加一个方法- (NSString *)apiForKey:(NSString *)key;
plist文件

image.png image.png

这部分测试肯定在测试环境需要测通,所以异常只在DEBUG模式

- (NSString *)apiForKey:(NSString *)key {
    id value = [self objectForKey:key];
    if (value == nil || value == [NSNull null])
    {
#if DEBUG
        @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"接口未处理%@", key] userInfo:nil];
#else
        return key;
#endif
    }
    return value;
}

一下子需要测试那么多接口,然后没有接口测试,没有测试了。
so,APP调试的时候需要把post和get的格式转一下,直接复制到postman使用,返回没有参数是为了醒目一点

+ (NSString*)changeToLogPostParameterStrWithDic:(NSDictionary*)dic {
    if (!dic.count) {
        return @"没有参数";
    }
    NSString* parameterStr = [dic mj_JSONString];
    if (parameterStr.length) {
        parameterStr = [parameterStr stringByReplacingOccurrencesOfString:@"{" withString:@""];
        parameterStr = [parameterStr stringByReplacingOccurrencesOfString:@"}" withString:@""];
        parameterStr = [parameterStr stringByReplacingOccurrencesOfString:@"\"" withString:@""];
        parameterStr = [parameterStr stringByReplacingOccurrencesOfString:@"," withString:@"\n"];
        return parameterStr;
    }
    return parameterStr;
}

+ (NSString*)changeToLogGetParameterStrWithDic:(NSDictionary*)dic {
    if (!dic.count) {
        return @"没有参数";
    }
    NSString* parameterStr = [NSString buildQueryWithDictionary:dic];
    return parameterStr;
}

+ (NSString *)buildQueryWithDictionary:(NSDictionary *) dictionary {
    NSMutableArray *query = [NSMutableArray array];
    for (NSString *key in [dictionary allKeys]) {
        id value = dictionary[key];
        [query addObject:[NSString stringWithFormat:@"%@=%@",
                          [key description],
                          [value description]]];
    }
    return [query componentsJoinedByString:@"&"];
}
image.png image.png

相关文章

  • 记一次runtime尝试

    尝试:runtime取到类方法,遍历调用类方法,结论:如果方法参数没有问题的话,是可以的。由于后期改变,最后参数没...

  • 事岀无常必有妖-iOS捉妖记之(Runtime)

    事岀无常必有妖-iOS捉妖记之(Runtime) 事岀无常必有妖-iOS捉妖记之(Runtime)

  • Runtime补救措施

    原文地址 :Runtime那些事儿(消息机制) 总结:当找不到对应的IML方法时,RunTime会尝试通过用户设定...

  • 你还记得多少的第一次

    你还记得多少,记得你曾经的青葱稚嫩,你还记多少你的第一次尝试的胆怯,第一次尝试的艰难,第一次尝试的喜悦,尝试成功的...

  • p4学习笔记-3

    接下来是p4Runtime的尝试 使用P4Runtime实现控制平面 介绍 在本练习中,我们将使用P4Runtim...

  • Java中一个boolean占用内存计算

    先上代码 解析: Runtime.getRuntime().maxMemory() Java虚拟机将尝试使用的最大...

  • runtime-第一篇

    第一次接触runtime,先介绍下自学的几个runtime方法 1.获取类的属性列表 先导入runtime文件 #...

  • 记一次教学小尝试

    三考以后,我们正式开始备战话题作文。话题作文呈现的第一亮点就当是学生所拟的题目,所谓“题好一半文”说的不就是这个嘛...

  • 记一次go尝试经历

    初学go,确实go在网络开发方面提供的很多便捷 自动化的编译工具,一个go build搞定一切 丰富的标准库,几乎...

  • 记一次Runtime Hook的问题

    背景 项目中遇到一个问题,需要引入两个SDK,我们暂且命名为A 和 B,由于业务需要这两个SDK都需要对一个系统函...

网友评论

      本文标题:记一次runtime尝试

      本文链接:https://www.haomeiwen.com/subject/pmbzyxtx.html