Photo by STIL on Unsplash

From an app to a Super App

Part 2: Setting the App in Frameworks

Laying the foundations

--

In the second part of this series: How to create a Super App and not die trying, we are going to learn how to split an app into frameworks. Why? In the previous post we talked about Organizing the app because at Jeff we now have more than one business model so we decided to split the project into modules to manage the app in an easy way, so each module is a business model and all of them are connected in the same app.

PROS

  • It is an easy way to develop an independent app.
  • It gives you the opportunity to release an app with only the business model you want.
  • It helps the development because each developer is inside an independent app.

CONS

  • You need a common module with the common features of the app.

To explain the process we are going to create a project: School Project, where we gonna have the main app: School App. In this project we are going to add as a pod the framework we are going to create: Student Framework, that it will be a development pod.

The main project

The first step is to create a Xcode project single view App. We are going to call it School Project. Let’s create a folder for our Home view from which we are going to navigate to our framework.

School project folders and files

As we said in the first post, we use MVVM with routers, so we instantiate the router of the home on SceneDelegate file and launch the app to check it works.

SceneDelegate.swift

Let’s create a Framework

Our framework is going to be called Student Framework. This is the pod we are going to use in our School App, but for developing it we are going to create an independent app: Student App, that is going to be an App Target inside Student Project. When we finish the framework and check that it is working on the Student App, we will import it to the main app as a pod. Here we have an explanation of how we are going to use our framework:

Student Framework

Now is the time for the first framework. Let’s create it! Go to File -> New -> Project -> Framework. We call it Student Framework. We are going to save it at the same folder level as the School Project for example:

Let’s create a folder: Views, with another two folders: Home and Detail. Inside each folder, we create the files for the views:

Student framework folders and files

Add a label in Student Detail view to identify the controller when it is shown. Here we have an important issue: in it the controllers with the correct bundle. If we don’t, the app will crash when we try to load the view.

SchoolHomeViewController.swift init method

Let’s create a button in the home view to show the detail:

SchoolHomeViewController.swift button method
SchoolHomeViewModel.swift method
SchoolHomeRouter.swift method

Student App

Once we have our framework prepared, it is time to use it and for doing that we are going to create an App inside Student project. Selecting the project on Xcode, we add a template for a new target: Single View App, and we call it Student App.

Create Single View App Target in Student Framework

When we create it, the App files appear on the project navigator. Now we have to add the Student Framework to the App: select StudentApp target -> General -> Frameworks, Libraries and Embedded Content, and add the framework.

Adding Student Framework in Student App

Select Student App as an active scheme and compile it. Open SceneDelegate file and import the framework to initialize the App. When we try to access the Student Home to initialize the App, we can not do so because it is not public. To solve that, go to Student Framework and create a folder called Interactor, where we will create a class that interacts with School Project. This class will be in charge of communicating the external App that is using the Framework with the Framework. So create a new swift file and call it StudentInteractor.

Creating StudentInteractor.swift file

This class will be public for access from out of the Framework. Inside it, we are going to create a public variable to get the StudentHomeViewController. The variable will be a UIViewController, because if it is StudentHomeViewController type, we must do the claas as public, and what we want is to maintain the framework privacy. The class would be as follows:

StudentInteractor.swift

After that, compile the Framework scheme and now we can access the HomeViewController from the project. We initialize the App with a UINavigationController, whose root view controller is the home, to permit the navigation to the Framework detail.

SceneDelegate.swift (Student App)

Launch the App and see the results. Now we can see the application launch the home of Student Framework and from there we can navigate to the detail.

Student Framework Podspec

The next step is to load our Student Framework into the School App. We are going to use Cocoapods to create a development pod. Open the Terminal and navigate to the project folder, after that, write “pod spec create StudentFramework” and launch it. The Framework folder will be as follows:

We open the podspec file created with any text editor and configure it. By default there are many parameters configured but we have to add a description, the license, specify the source of the pod (as it is a development framework we will put: :path => ‘.’ ), the resources and classes that we want to be accessible and those that are not. We’ll also indicate that we want the folder hierarchy to be maintained. After finishing the podspec, in the terminal we go to the School project folder and do “pod init”. We open the pod file created and add our framework.

School Project Podfile

We make a “pod install” and open the workspace of the project, where the corresponding folder of the pods will have been generated and inside it one that contains the development pods. We compile the project to check that everything is correct. Now we go to the SchoolHomeViewController file, where we create a button that opens the Student home. We will do the same steps as in the Student Home view to go into detail. Finally, in the SchoolHomeRouter file, we will import the framework to be able to access and navigate to its home page.

Launch the app and see the result.

Gif of the result of School App

What’s next?

You can find the source code of the example on GitHub.

We have created our own framework, an app to develop the framework and we have accessed the framework from the main application. So much for this second part of How to create a super app and not die trying. Following these steps we can create all kinds of frameworks, either to have only logic or to have views that are reused throughout the application we are developing. In the next post we will see how to create development environments both in the project and in the different development frameworks we have.

--

--

Juantri Jiménez

Spanish iOS software engineer. I write about features of Swift and iOS development practices