iOS 设置一个简单的路由

324 阅读1分钟

.h中这样写

@property (nonatomic ,strong) NSDictionary *bridgeDic;

+ (EJAPPPushBridge *)shareInstance;

+ (BOOL)pushViewControllerWithUrlStr:(NSString *)urlStr;

.m中实现

+ (EJAPPPushBridge *)shareInstance {
    static EJAPPPushBridge *_pushBriage = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _pushBriage = [[EJAPPPushBridge alloc] init];
        //此处是本地的配置文件,也可以放在后台来设置
        _pushBriage.bridgeDic = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"EJControllerConfig" ofType:@"plist"]];
    });
    
    return _pushBriage;
}

+ (BOOL)pushViewControllerWithUrlStr:(NSString *)urlStr{
    NSURL *url =[NSURL URLWithString:urlStr];
    UIViewController *rootVC = [UIViewController currentViewController];

    if([url.scheme isEqualToString:@"http"] ||[url.scheme isEqualToString:@"https"]){
        EJWebViewController *webVC = [[EJWebViewController alloc]init];
        webVC.webURL = [NSURL URLWithString:urlStr];
        [rootVC.navigationController pushViewController:webVC animated:true];
    }else if([url.scheme isEqualToString:@"tel"]){
        [EJHelp pushCallWithPhoneNum:urlStr];
    } else {
        UIViewController *controller = [[EJAPPPushBridge shareInstance] guideVCWithUrl:url];
        //判断当前控制是不是在显示
        if ([rootVC isMemberOfClass:[controller class]]) {
//            if ([controller respondsToSelector:NSSelectorFromString(@"refreshCurrentView")]) {
//                [controller performSelector:NSSelectorFromString(@"refreshCurrentView")];
//            }
            if ([rootVC isKindOfClass:[EJGoodsDetailController class]]||
                [rootVC isKindOfClass:[EJOrderDetailViewController class]]) {
                [rootVC.navigationController pushViewController:controller animated:true];
            }else{
                [(EJBaseViewController *)rootVC  refreshCurrentView];
            }
        } else {
            [rootVC.navigationController pushViewController:controller animated:true];
        }
    }
    
    return NO;
}

- (UIViewController *)guideVCWithUrl:(NSURL *)url{
    if ([[url scheme] isEqualToString:@"EasyJoyShop"]){
        
        NSDictionary *params = [url paramsDict];
        NSString *path = [NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%@%@",url.host,url.path]];
        NSString *VCStr = _bridgeDic[path];
        return [self gudiVCWithVCStr:VCStr WithValueDic:params];
    }
    return nil;
}

- (UIViewController *)gudiVCWithVCStr:(NSString *)VCStr WithValueDic:(NSDictionary *)valueDic{
    UIViewController *bridgeVC = [[NSClassFromString(VCStr) alloc] init];
    if (bridgeVC && [valueDic isKindOfClass:[NSDictionary class]]) {
        [bridgeVC yy_modelSetWithDictionary:valueDic];
    }
    return bridgeVC;
}