Xcode File Template for generating VIPER modules: View, Interactor, Presenter, and Router. Written in Swift 5
- Clone the repository
- Navigate to Xcode Templates folder:
~/Library/Developer/Xcode/Templates/
. If Templates folder doesn't exist, create it - Copy and paste the VIPER Module.xctemplate in Templates folder
- Open Xcode
File -> New -> File
or⌘ N
- Scroll down till you see
VIPER Module
template and choose it. - Set a name for your module. Examples:
Home, Auth, SignIn
Let's suppose we wanted to create the Auth module. Here's how it would look:
AuthContract.swift
import Foundation
// MARK: View Output (Presenter -> View)
protocol PresenterToViewAuthProtocol {
}
// MARK: View Input (View -> Presenter)
protocol ViewToPresenterAuthProtocol {
var view: PresenterToViewAuthProtocol? { get set }
var interactor: PresenterToInteractorAuthProtocol? { get set }
var router: PresenterToRouterAuthProtocol? { get set }
}
// MARK: Interactor Input (Presenter -> Interactor)
protocol PresenterToInteractorAuthProtocol {
var presenter: InteractorToPresenterAuthProtocol? { get set }
}
// MARK: Interactor Output (Interactor -> Presenter)
protocol InteractorToPresenterAuthProtocol {
}
// MARK: Router Input (Presenter -> Router)
protocol PresenterToRouterAuthProtocol {
}
AuthViewController.swift
import UIKit
class AuthViewController: UIViewController {
// MARK: - Properties
var presenter: ViewToPresenterAuthProtocol?
// MARK: - Lifecycle Methods
override func viewDidLoad() {
super.viewDidLoad()
}
}
extension AuthViewController: PresenterToViewAuthProtocol{
// TODO: Implement View Output Methods
}
AuthInteractor.swift
import Foundation
class AuthInteractor: PresenterToInteractorAuthProtocol {
// MARK: Properties
var presenter: InteractorToPresenterAuthProtocol?
}
AuthPresenter.swift
import Foundation
class AuthPresenter: ViewToPresenterAuthProtocol {
// MARK: Properties
var view: PresenterToViewAuthProtocol?
var interactor: PresenterToInteractorAuthProtocol?
var router: PresenterToRouterAuthProtocol?
}
extension AuthPresenter: InteractorToPresenterAuthProtocol {
}
AuthRouter.swift
import Foundation
import UIKit
class AuthRouter: PresenterToRouterAuthProtocol {
// MARK: Static methods
static func createModule() -> UIViewController {
let viewController = AuthViewController()
let presenter: ViewToPresenterQuotesProtocol & InteractorToPresenterQuotesProtocol = AuthPresenter()
viewController.presenter = presenter
viewController.presenter?.router = AuthRouter()
viewController.presenter?.view = viewController
viewController.presenter?.interactor = AuthInteractor()
viewController.presenter?.interactor?.presenter = presenter
return viewController
}
}