iOS撸一个简单路由Router的实现代码

2020-01-21 07:23:16丽君

这自然要归功于Runtime的强大力量,我们先看如何实现吧。


@implementation SJRouter 
- (instancetype)init {
  self = [super init];
  if ( !self ) return nil;
  _handlersM = [NSMutableDictionary new];
  int count = objc_getClassList(NULL, 0);
  Class *classes = (Class *)malloc(sizeof(Class) * count); objc_getClassList(classes, count);
  Protocol *p_handler = @protocol(SJRouteHandler);
  for ( int i = 0 ; i < count ; ++ i ) {
    Class cls = classes[i];
    for ( Class thisCls = cls ; nil != thisCls ; thisCls = class_getSuperclass(thisCls) ) {
      if ( !class_conformsToProtocol(thisCls, p_handler) ) continue;
      if ( ![(id)thisCls respondsToSelector:@selector(routePath)] ) continue;
      if ( ![(id)thisCls respondsToSelector:@selector(handleRequestWithParameters:topViewController:completionHandler:)] ) continue;
      _handlersM[[(id<SJRouteHandler>)thisCls routePath]] = thisCls;
      break;
    }
  }
  if ( classes ) free(classes);
  return self;
}
@end
  1. objc_getClassList: 很明显了, 获取App所有类。
  2. class_conformsToProtocol: 该类是否遵守某个协议。

得益于Runtime的这两个函数,即可获取到众多的Handler。 当发起请求时, 在众多Handler中寻找注定的那一个, 岂不是易如反掌。

Request

App发起的跳转请求,更多到是内部页面之间的跳转, 我们最需要关注的就是它的路径,所以整个路由都是围绕着路径去跳转的, 而像URL中的scheme和host,体现出来的作用倒是不大。至少在我的项目中跳转第三方App(例如分享)都是使用的友盟这类的SDK去处理的。

因此, Request持有每个请求的路径, 以及必要的参数, 之后再无多余操作。 

好了, 就到这里了。