美文网首页
记一次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尝试

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