SkillAgentSearch skills...

ZIKRouter

Interface-oriented router for discovering modules, and injecting dependencies with protocol in Objective-C and Swift.

Install / Use

/learn @Zuikyo/ZIKRouter
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

<p align="center"> <img src="Documentation/Resources/icon.png" width="33%"> </p>

ZIKRouter

ZIKRouter Carthage compatible license

An interface-oriented router for managing modules and injecting dependencies with protocol.

The view router can perform all navigation types in UIKit / AppKit through one method.

The service router can discover and prepare corresponding module with its protocol.


一个用于模块间解耦和通信,基于接口进行模块管理和依赖注入的组件化路由工具。用多种方式最大程度地发挥编译检查的功能。

通过 protocol 寻找对应的模块,并用 protocol 进行依赖注入和模块通信。

Service Router 可以管理任意自定义模块。View Router 进一步封装了界面跳转。

中文文档


Features

  • [x] Swift and Objective-C support
  • [x] iOS, macOS and tvOS support
  • [x] File template for quickly creating router
  • [x] Routing for UIViewController / NSViewController, UIView / NSView and any class
  • [x] Dependency injection, including dynamic injection and static injection
  • [x] Declaration of routable protocol for compile-time checking. Using undeclared protocol will bring compiler error. This is one of the most powerful feature
  • [x] Module matching with its protocol
  • [x] URL routing support
  • [x] Configure the module with its protocol rather than a parameter dictionary
  • [x] Required protocol and provided protocol for making thorough decouple
  • [x] Adapter for decoupling modules and add compatible interfaces
  • [x] Storyboard support. Views from a segue can be auto prepared
  • [x] Encapsulation for all transition methods and unwind methods in UIKit / AppKit, and also custom transition
  • [x] Error checking for view transition
  • [x] AOP for view transition
  • [x] Memory leak detection
  • [x] Custom events handling
  • [x] Auto registration
  • [x] Highly scalable

Quick Start Guide

  1. Create Router
    1. Router Subclass
    2. Simple Router
  2. Declare Routable Type
  3. View Router
    1. Transition directly
    2. Prepare before Transition
    3. Make Destination
    4. Required Parameter and Special Parameter
    5. Perform on Destination
    6. Prepare on Destination
    7. Remove
    8. Adapter
    9. Modularization
    10. URL Router
    11. Other Features
  4. Service Router
  5. Demo and Practice
  6. File Template

Documentation

Design Idea

Design Idea

Basics

  1. Router Implementation
  2. Module Registration
  3. Routable Declaration
  4. Type Checking
  5. Perform Route
  6. Remove Route
  7. Transfer Parameters with Custom Configuration

Advanced Features

  1. Error Handle
  2. Storyboard and Auto Create
  3. AOP
  4. Dependency Injection
  5. Circular Dependency
  6. Module Adapter
  7. Unit Test

FAQ

Requirements

  • iOS 7.0+
  • Swift 3.2+
  • Xcode 9.0+

Installation

Cocoapods

Add this to your Podfile.

For Objective-C project:

pod 'ZIKRouter', '>= 1.1.1'

# or only use ServiceRouter
pod 'ZIKRouter/ServiceRouter' , '>=1.1.1'

For Swift project:

pod 'ZRouter', '>= 1.1.1'

# or only use ServiceRouter
pod 'ZRouter/ServiceRouter' , '>=1.1.1'

Carthage

Add this to your Cartfile:

github "Zuikyo/ZIKRouter" >= 1.1.1

Build frameworks:

carthage update

Build DEBUG version to enable route checking:

carthage update --configuration Debug

Remember to use release version in production environment.

For Objective-C project, use ZIKRouter.framework. For Swift project, use ZRouter.framework.

Getting Started

This is the demo view controller and protocol:

///Editor view's interface
protocol EditorViewInput: class {
    weak var delegate: EditorDelegate? { get set }
    func constructForCreatingNewNote()
}

///Editor view controller
class NoteEditorViewController: UIViewController, EditorViewInput {
    ...
}
<details><summary>Objective-C Sample</summary>
///editor view's interface
@protocol EditorViewInput <ZIKViewRoutable>
@property (nonatomic, weak) id<EditorDelegate> delegate;
- (void)constructForCreatingNewNote;
@end
///Editor view controller
@interface NoteEditorViewController: UIViewController <EditorViewInput>
@end
@implementation NoteEditorViewController
@end
</details>

There're 2 steps to create route for your module.

1. Create Router

To make your class become modular, you need to create router for your module. You don't need to modify the module's code. That will reduce the cost for refactoring existing modules.

1.1 Router Subclass

Create router subclass for your module:

import ZIKRouter.Internal
import ZRouter

class NoteEditorViewRouter: ZIKViewRouter<NoteEditorViewController, ViewRouteConfig> {
    override class func registerRoutableDestination() {
        // Register class with this router. A router can register multi views, and a view can be registered with multi routers
        registerView(NoteEditorViewController.self)
        // Register protocol. Then we can fetch this router with the protocol
        register(RoutableView<EditorViewInput>())
    }
    
    // Return the destination module
    override func destination(with configuration: ViewRouteConfig) -> NoteEditorViewController? {
        // In configuration, you can get parameters from the caller for creating the instance
        let destination: NoteEditorViewController? = ... /// instantiate your view controller
        return destination
    }
    
    override func prepareDestination(_ destination: NoteEditorViewController, configuration: ViewRouteConfig) {
        // Inject dependencies to destination
    }
}
<details><summary>Objective-C Sample</summary>
//NoteEditorViewRouter.h
@import ZIKRouter;

@interface NoteEditorViewRouter : ZIKViewRouter
@end

//NoteEditorViewRouter.m
@import ZIKRouter.Internal;

@implementation NoteEditorViewRouter

+ (void)registerRoutableDestination {
    // Register class with this router. A router can register multi views, and a view can be registered with multi routers
    [self registerView:[NoteEditorViewController class]];
    // Register protocol. Then we can fetch this router with the protocol
    [self registerViewProtocol:ZIKRoutable(EditorViewInput)];
}

// Return the destination module
- (NoteEditorViewController *)destinationWithConfiguration:(ZIKViewRouteConfiguration *)configuration {
    // In configuration, you can get parameters from the caller for creating the instance 
    NoteEditorViewController *destination = ... // instantiate your view controller
    return destination;
}

- (void)prepareDestination:(NoteEditorViewController *)destination configuration:(ZIKViewRouteConfiguration *)configuration {
    // Inject dependencies to destination
}

@end
</details>

Each router can control their own routing, such as using different custom transition. And the router can be very easy to add additional features.

Read the documentation for more details and more methods to override.

1.2 Simple Router

If your module is very simple and don't need a router subclass, you can just register the class in a simpler way:

ZIKAnyViewRouter.register(RoutableView<EditorViewInput>(), forMakingView: NoteEditorViewController.self)
<details><summary>Objective-C Sample</summary>
[ZIKViewRouter registerViewProtocol:ZIKRoutable(EditorViewInput) forMakingView:[NoteEditorViewController class]];
</details>

or with custom creating block:

ZIKAnyViewRouter.register(RoutableView<EditorViewInput>(), 
                 forMakingView: NoteEditorViewController.self) { (config, router) -> EditorViewInput? in
                     let destination: NoteEditorViewController? = ... // instantiate your view controller
                     return destination;
        }

<details><summary>Objective-C Sample</summary>
[ZIKViewRouter
    registerViewProtocol:ZIKRoutable(EditorViewInput)
    forMakingView:[NoteEditorViewController class]
    making:^id _Nullable(ZIKViewRouteConfiguration *config, ZIKViewRouter *router) {
        NoteEditorViewController *destination = ... // instantiate your view controller
        return destination;
 }];
</details>

or with custom factory function:

function makeEditorViewController(config: ViewRouteConfig) -> EditorViewInput? {
    let destination: NoteEditorViewController? = ... // instantiate your view controller
    return destination;
}

ZIKAnyViewRouter.register(RoutableView<EditorViewInput>(), 
                 forMakingView: NoteEditorViewController.self, making: makeEditorViewController)
<details><summary>Objective-C Sample</summary>
id<Edito
View on GitHub
GitHub Stars659
CategoryDevelopment
Updated16d ago
Forks115

Languages

Objective-C

Security Score

100/100

Audited on Mar 15, 2026

No findings