Modular Architecture with MVVM-C + SPM + SwiftUI in iOS

Basic implementation of the Modular Architecture using the most modern tools

Bruno Chen Chih Ying
3 min readMay 15, 2021

Why use modules?

The answer is easy, do you remember when you were a child and played legos? So, Lego is the most scalable and recyclable toy of all times!! You can build anything with your Legos, like a car, and you can use the same Legos to make a boat. It’s amazing!!

The modules are like Legos, they are mini isolated parts of the application. You can reuse the modules in infinite projects, as if they were a library!!

This post will help you to build the base MVVM-c modular architecture with Swift Package Manager (SPM) and SwiftUI. Let’s start!!

Theory

We’ll use the coordinator to connect and manage all of our features flows, and the features will use the commons module and Design System module, just like in a real situation!!

The big problem is the features need to know what Coordinator is, and the Coordinator needs to know all the features, but we can’t have cycle dependency between modules 🤔 !! So, the trick is to create a Coordinator protocol in the Commons module!!

STEP 1- Create Commons SPM

First we need to create the Commons layers to add coordinator protocols!!

  1. Select “File > Swift Package”

2. Create “Package” folder in your project to save the SPM modules, and add the SPM to your app

3. Add Coordinator SPM to “App>App Targets>Frameworks”

4. Set platforms in the Package file

let package = Package(
name: "Commons",
platforms: [.iOS(.v13)], //<<<<< HERE

5. Create Coordinator.swift to add protocols

import SwiftUI
public protocol Coordinator: AnyObject {
var navigationController: UINavigationController? {get set}
init(navigationController: UINavigationController?)
}

STEP 2- Create Coordinator SPM

Now we need to create a Coordinator layer to manage our application flow!!

1. Create Home SPM (see Step 1- item 1,2,3,4)

2. Create MainCoordinator.swift file to add the basic coordinator class codes. We will send the navigationController to the Home feature (STEP 4) and start it

STEP 3- Root app call the Coordinator SPM

  1. For iOS13 & Xcode 11 or earlier, we need to use the SceneDelegate class. We need to create a UINavigationController, put inside the window.rootViewController and distribute to Coordinator SPM.

STEP 4- Create Home Feature

  1. Create Home SPM (see Step 1- item 1,2,3,4)

2. Create HomePageCoordinator.swift, implement the Coordinator protocol. In the start methods, instantiate the HomePageViewModel and the HomePageView, put the HomePageView to the navigationController.setViewControllers

3. Create the Home Page ViewModel, here we can call the navigation methods (navigationDelegate) of the MainCoordinator

4. Create a simple view of the Home Page

5. Add HomeCoordinatorDelegate extension and wantsToNavigateToFirstFeature methods in the MainCoordinator.swift , that’s methods is responsible to navigate to the Fist Feature

NEXT STEPS

This post shows the implementation of one feature, if you want to see more features in this project, you can visit my GitHub Project!!

--

--