<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Murat Emre Aygün on Medium]]></title>
        <description><![CDATA[Stories by Murat Emre Aygün on Medium]]></description>
        <link>https://medium.com/@muratemreaygun?source=rss-4efc24a8328b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*ndRkv4YdMyoOVhH_</url>
            <title>Stories by Murat Emre Aygün on Medium</title>
            <link>https://medium.com/@muratemreaygun?source=rss-4efc24a8328b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 04:06:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@muratemreaygun/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Coordinator Pattern for iOS Apps]]></title>
            <link>https://medium.com/appcent/coordinator-pattern-for-ios-apps-52d4627373bf?source=rss-4efc24a8328b------2</link>
            <guid isPermaLink="false">https://medium.com/p/52d4627373bf</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[ios-development]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Murat Emre Aygün]]></dc:creator>
            <pubDate>Mon, 11 Dec 2023 11:47:08 GMT</pubDate>
            <atom:updated>2023-12-13T14:31:19.748Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NdFs_8tcIfvakCRDbLIsTQ.png" /></figure><p>As you know in mobile applications we are frequently navigating between screens and while navigating we are basically getting the instance of the view controller that we want to navigate to:</p><pre>class ViewController: UIViewController {<br>   private func goToProductDetailScreen() {<br>      let storyboard = UIStoryboard.init(name: &quot;Main&quot;, bundle: nil)<br>      guard let vc = storyboard.instantiateViewController(withIdentifier: &quot;ProductDetailVC&quot;) as? ProductDetailVC else { return }<br>      self.navigationController?.pushViewController(vc, animated: true)<br>   }<br>}</pre><p>So if we evaluate this code sample we can see that it satisfies our needs to go to the screen we want. But when we try to go to the same screen from different view controllers we should duplicate the code block and use it in multiple places. As the app grows we will encounter with these kind of situations more frequently and will use more of the duplicate code for navigation purposes. That means it will be more challenging to manage it as the complexity of the app grows.</p><p>For this case the Coordinator Pattern emerges as a useful solution to separate navigation logic and increase the modularity of our code. In this brief publication, we will examine the Coordinator Pattern and its implementation in Swift.</p><h3>Understanding the Coordinator Pattern</h3><p>The Coordinator Pattern is a design pattern that aims to centralize and manage the navigation flow in an iOS application. It provides us a clean architecture by separating the responsibilities of view controllers, making them more focused on their specific tasks.</p><h3>Implementation in Swift</h3><p>So let’s dive into the Coordinator Pattern with an example. At our example we are going to have an app which has a tabbar controller with two tab items (Home and profile screens respectively).</p><p>First of all we should define a protocol as shown below to be used at our separated coordinator classes for different navigation flows:</p><pre>import UIKit<br><br>protocol Coordinator: AnyObject {<br>    /// Delegate to be triggered when a view controller&#39;s coordinator is disappearing to notify previous coordinator<br>    var finishDelegate: CoordinatorFinishDelegate? { get set }<br>    /// Each coordinator has one navigation controller assigned to it.<br>    var navigationController: UINavigationController { get set }<br><br>    /// Array to keep tracking of all child coordinators.<br>    var childCoordinators: [Coordinator] { get set }<br>    <br>    /// Defined flow type.<br>    var type: CoordinatorType { get }<br><br>    func start()<br><br>    /// A place to put logic to finish the flow, to clean all children coordinators, and to notify the parent that this coordinator is ready to be deallocated<br>    func finish()<br>}<br><br>extension Coordinator {<br>    func finish() {<br>        /// For default stuff for completely reseting all flow<br>        childCoordinators.removeAll()<br>        finishDelegate?.coordinatorDidFinish(childCoordinator: self)<br>    }<br>}<br><br>// MARK: - CoordinatorOutput<br><br>protocol CoordinatorFinishDelegate: class {<br>    func coordinatorDidFinish(childCoordinator: Coordinator)<br>}<br><br>// MARK: - CoordinatorType<br><br>enum CoordinatorType {<br>    case app, mainTab, tabItem /// for our example we will only have this ones but normally as the project gets bigger we will use more screens which means more coordinators. So the more cases should be added in that scenario.<br>}</pre><p>After defining the Coordinator protocol we should create a coordinator class which will be used as the initial coordinator at our application launch. This coordinator class will conform to a protocol that we defined above but with additional function. So we are creating a new protocol that inherits from our previously defined <strong><em>Coordinator protocol </em></strong>and use it for our new class:</p><pre>import UIKit<br><br>protocol AppCoordinatorProtocol: Coordinator {<br>    func showMainFlow()<br>}<br><br>class AppCoordinator: AppCoordinatorProtocol {<br>    weak var finishDelegate: CoordinatorFinishDelegate?<br><br>    var navigationController: UINavigationController<br><br>    var childCoordinators = [Coordinator]()<br><br>    var type: CoordinatorType { .app }<br><br>    init(navigationController: UINavigationController) {<br>        self.navigationController = navigationController<br>    }<br>    <br>    func start() {<br>        self.showMainFlow()<br>    }<br><br>    func showMainFlow() {<br>        let tabCoordinator = TabCoordinator(navigationController)<br>        tabCoordinator.finishDelegate = self<br>        tabCoordinator.start()<br>        childCoordinators.append(tabCoordinator)<br>    }<br>}<br><br>extension AppCoordinator: CoordinatorFinishDelegate {<br>    func coordinatorDidFinish(childCoordinator: Coordinator) {<br>        // Do additional stuff after the presented screen is dismissed <br>        childCoordinators = childCoordinators.filter { $0.type != childCoordinator.type }<br>    }<br>}</pre><p>And we will use this initial coordinator at our <strong><em>AppDelegate</em></strong> by defining an <strong><em>appCoordinator</em></strong> property and setting it in <strong><em>AppDelegate’s</em></strong> <strong><em>didFinishLaunchingWithOptions</em></strong> method:</p><pre>@main<br>class AppDelegate: UIResponder, UIApplicationDelegate {<br>    var window: UIWindow?<br>    var appCoordinator: AppCoordinator?<br><br>func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -&gt; Bool {<br>    appCoordinator = AppCoordinator(navigationController: UINavigationController())<br>    appCoordinator?.start()<br><br>    window = UIWindow(frame: UIScreen.main.bounds)<br>    window?.rootViewController = navController<br>    window?.makeKeyAndVisible()<br><br>    return true<br>}<br><br>...<br>}</pre><p>So to summarize we created a initial Coordinator which will handle the first navigation flow and showed a tabbar controller with triggering the <strong><em>showMainFlow() </em></strong>function. This function opens another coordinator for Tabbar arrangements which is named TabCoordinator:</p><pre>enum TabBarPage {<br>    case home<br>    case profile<br><br>    func getIndex() -&gt; Int {<br>        switch self {<br>            case .home: return 0<br>            case .profile: return 1<br>        }<br>    }<br>    <br>    func getTitleName() -&gt; String {<br>        switch self {<br>        case .home:<br>            return &quot;Home&quot;<br>        case .profile:<br>            return &quot;Profile&quot;<br>        }<br>    }<br>    <br>    func getIconName() -&gt; String {<br>        switch self {<br>        case .home:<br>            return &quot;iconHome&quot;<br>        case .profile:<br>            return &quot;iconUser&quot;<br>        }<br>    }<br>}<br><br>protocol TabCoordinatorProtocol: Coordinator {<br>    var tabBarController: UITabBarController { get set }<br>    func selectPage(_ page: TabBarPage)<br>    func setSelectedIndex(_ index: Int)<br>    func currentPage() -&gt; TabBarPage?<br>}<br><br>class TabCoordinator: NSObject, Coordinator {<br>    weak var finishDelegate: CoordinatorFinishDelegate?<br><br>    var childCoordinators: [Coordinator] = []<br><br>    var navigationController: UINavigationController<br><br>    var tabBarController: TabBarController<br><br>    var type: CoordinatorType { .mainTab }<br><br>    init(navigationController: UINavigationController) {<br>        self.navigationController = navigationController<br>        tabBarController = UITabBarController()<br>    }<br>    <br>    func start() {<br>        let pages: [TabBarPage] = [.home, .profile]<br><br>        // Initialization of ViewControllers from pages<br>        let controllers: [UINavigationController] = pages.map { getTabController($0) }<br><br>        prepareTabBarController(withTabControllers: controllers)<br>    }<br><br>    private func getTabController(_ page: TabBarPage) -&gt; UINavigationController {<br>        let navController = UINavigationController()<br>        navController.tabBarItem = UITabBarItem(title: page.getTitleName(),<br>                                                image: UIImage(named: page.getIconName()),<br>                                                tag: page.getIndex())<br>        switch page {<br>            case .home:<br>                let coordinator = HomeCoordinator(navController)<br>                coordinator.start()<br>                childCoordinators.append(coordinator)<br>            case .profile:<br>                let coordinator = ProfileCoordinator(navController)<br>                coordinator.start()<br>                childCoordinators.append(coordinator)<br>        }<br>        return navController<br>    }<br>    private func prepareTabBarController(withTabControllers tabControllers: [UIViewController]) {<br>        tabBarController.delegate = self<br>        tabBarController.setViewControllers(tabControllers, animated: true)<br>        tabBarController.selectedIndex = TabBarPage.home.getIndex()<br>        tabBarController.tabBar.isTranslucent = false<br><br>        /// In this step, we attach tabBarController to navigation controller associated with this coordanator<br>        navigationController.viewControllers = [tabBarController]<br>    }<br>}</pre><p>Lastly, for this example we used only two tab items which are <strong><em>home and profile. </em></strong>So for them we are using another <em>CoordinatorType</em> which is .tabItem and here is how the coordinators for those items look:</p><pre>class HomeCoordinator: Coordinator {<br>    weak var finishDelegate: CoordinatorFinishDelegate?<br><br>    var navigationController: UINavigationController<br><br>    var childCoordinators: [Coordinator] = []<br><br>    var type: CoordinatorType = .tabItem<br><br>    var homeVC: HomeVC<br><br>    init(navigationController: UINavigationController) {<br>        self.navigationController = navigationController<br>        homeVC = HomeVC.instantiate() // Suppose that we have an extension which has instantiate function for creating view controllers. At this publication I didn&#39;t show the content of it.<br>        homeVC.delegate = self<br>    }<br><br>    func start() {<br>        navigationController.pushViewController(homeVC, animated: true)<br>    }<br>}<br><br>extension HomeCoordinator: CoordinatorFinishDelegate {<br>    func coordinatorDidFinish(childCoordinator: Coordinator) {<br>        childCoordinators = childCoordinators.filter { $0.type != childCoordinator.type }<br>    }<br>}<br><br>extension HomeCoordinator: HomeVCDelegate {<br>    func goToProductDetail() {}<br>    func goToProductList() {}<br>    ...<br>}</pre><pre>class ProfileCoordinator: Coordinator {<br>    weak var finishDelegate: CoordinatorFinishDelegate?<br><br>    var navigationController: UINavigationController<br><br>    var childCoordinators: [Coordinator] = []<br><br>    var type: CoordinatorType = .tabItem<br><br>    var profileVC: ProfileVC<br><br>    init(navigationController: UINavigationController) {<br>        self.navigationController = navigationController<br>        profileVC = ProfileVC.instantiate()  // Suppose that we have an extension which has instantiate function for creating view controllers. At this publication I didn&#39;t show the content of it.<br>        profileVC.delegate = self<br>    }<br><br>    func start() {<br>        navigationController.pushViewController(vc, animated: false)<br>    }<br>}<br><br>extension ProfileCoordinator: CoordinatorFinishDelegate {<br>    func coordinatorDidFinish(childCoordinator: Coordinator) {<br>        childCoordinators = childCoordinators.filter { $0.type != childCoordinator.type }<br>    }<br>}<br><br>extension ProfileCoordinator: ProfileVCDelegate {<br>    func goToSettings() {}<br>    func goToFavorites() {}<br>    ...<br>}</pre><p>With these codes it can be said that we gave a short summary of how to implement Coordinator Pattern at an iOS Application. So what is the benefit of this?</p><ul><li>The Coordinator Pattern separates navigation logic from view controllers which means increased modularity.</li><li>Code duplication in different view controllers for navigation flows become hindered.</li><li>Coordinators can be reused in different parts of the application.</li><li>Navigation logic becomes easier to test, as coordinators can be tested independently.</li><li>The pattern scales well with the growth of the application, making it easy to add and manage new features and flows</li></ul><h3>Conclusion</h3><p>In conclusion, the Coordinator Pattern is a powerful tool for managing navigation in iOS applications, promoting a clean and modular architecture. By using different coordinators for distinct navigation flows, we can maintain a structured and scalable codebase. While there is a learning curve, the benefits of improved separation of concerns, reusability, and easy testing make the Coordinator Pattern a valuable addition to your iOS projects. So using the Coordinator Pattern allows us to develop more robust and maintainable iOS applications.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=52d4627373bf" width="1" height="1" alt=""><hr><p><a href="https://medium.com/appcent/coordinator-pattern-for-ios-apps-52d4627373bf">Coordinator Pattern for iOS Apps</a> was originally published in <a href="https://medium.com/appcent">Appcent</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>