apple ios

iOS Architecture: Exploring RIBs

The excellent factor, you don’t have to write down a boilerplate code each and every time you wish to have to create a brand new RIB with the entire parts. You can set up and configure the Xcode template. To create a brand new RIB, you merely want to open a document advent menu and make a choice RIB from the checklist:

RIBs template for Xcode

We will create a RIB referred to as Login, and test a RIB that owns a view:

Xcode template generates 4 recordsdata. We will undergo every of them and talk about how they serve as.


As we all know, a builder is chargeable for growing the entire RIB parts.
Note, all the code under is auto-generated for you via the Xcode template.

The very first thing you’ll understand is that lots of the parts are protocols, now not the concrete categories. This is one of the foremost RIBs options, that we can talk about later on this article.

LoginDependency is used to inject dependencies into the RIB from its guardian. For instance, we now have a webService that we use to accomplish a login web-request. We create a WebServicing protocol we need to inject:

Now we will be able to replace the LoginDependency protocol, offering builder get admission to to its dependencies:

The subsequent part that we’ve got right here, is a LoginElement. We can claim some native variables that we handiest use inside of this Builder, reminiscent of settings, or AdMob ID, and many others. For our instance, we can go away this elegance as is, as a result of we don’t want any personal dependencies.

The subsequent protocol is a LoginBuildable that has handiest one manner construct(with listener:). The listener it takes as a parameter is a guardian listener. We are loose so as to add extra parameters to this construct manner, because it’s appropriate for our common sense.
LoginBuilder elegance implements LoginBuildable and it’s a significant part right here. It makes use of the LoginDependency to create a LoginElement.

LoginElement now encapsulates the entire dependencies we’d like for this RIB. The builder additionally creates a LoginViewController, LoginInteractor, this is used to create and go back a LoginRouter.

Here is any other necessary line of code:

interactor.listener = listener 

This is how we attach the guardian interactor with a kid interactor. For instance, we now have a LoginRIB that is attached to a RootRIB. In this example RootInteractor must put into effect the strategies, that LoginInteractor listener will claim. If LoginInteractor says brush asideLogin, root RIB will put into effect this technique to detach Login go with the flow and provide a house web page.

We will go back to Router later after we want to use a few of its dependencies, for now we transfer to the following part — Interactor.


Again, all the code under is auto-generated for you via the Xcode template.

LoginRouting is the protocol we use to navigate from Login RIB to next RIBs. Let’s say, we would like in an effort to navigate to CreateAccount display:

LoginPresentable is used to replace the Login view in keeping with industry common sense, carried out inside the interactor. If you open LoginViewController, you’ll understand, that it implements this protocol. LoginPresentable additionally owns a LoginPresentableListener . This is some way that LoginViewController will be in contact with the interactor and invoke industry common sense. In different phrases, this is how Interactor and ViewController be in contact with every different:

As we mentioned above, we would like our view controller to turn an process indicator when the web-task is in development. To put into effect this, we upload a brand new manner showActivityIndicator to LoginPresentable:

Finally, we now have a LoginListener . Remember this line of code in LoginBuilder?

interactor.listener = listener

This is a listener {that a} Root RIB will put into effect. This is some way for a kid RIB to be in contact again to its guardian. We want to notify the Root RIB when login is done, so login go with the flow will also be brushed aside:

Now we have a look at LoginInteractor elegance. It has two susceptible variables, router, and listener. This is how the interactor is attached to its router and guardian interactor respectively. You’ve noticed about, that interactor additionally owns a presenter.

As you consider, the core thought in the back of RIBs is that the app will have to be pushed via industry common sense. An interactor is a spot the place this industry common sense lives.

Here is how we use interactor to keep an eye on the app go with the flow:

  • we invoke presenter how to replace Login UI (we now have showActivityIndicator in our instance)
  • we invoke router how to navigate to the kid RIBs (we now have pathToCreateAccount in our instance)
  • we invoke listener how to communicate to guardian RIB (we now have brush asideLoginDrift in our instance)

Next we see a couple of lifecycle-managed strategies, didBecomeActive and willResignActive . These strategies are self-explanatory, and we don’t name them at once. For instance, we will be able to carry out a web-task in didBecomeActive to fetch required information, or make an preliminary view setup relying on our industry common sense.

We will go back to interactor later, now let’s end with the remainder parts — router, view, and presenter


Again, all the code under is auto-generated for you via the Xcode template.

LoginInteractable is the principle protocol right here, that incorporates two parts, LoginRouting and LoginListener . We created either one of them in Interactor.

LoginViewControllable is used to control the view hierarchy. So when the Interactor tells a router to navigate to CreateAccount the usage of LoginRouting , the router will sooner or later want to provide a CreateAccount display. We want to upload the next manner:

As you’ll be able to see, LoginRouter implements LoginRouting protocol, so we want to upload a required manner pathToCreateAccount:

We want to have the CreateAccount RIB earlier than we will be able to provide its viewController. Go forward and create any other RIB.

We aren’t going to make any adjustments on this RIB, so simply go away it there and go back to LoginRouter .

To to construct a CreateAccount RIB, LoginRouter must have CreateAccountBuilder. Declare a personal variable of kind CreateAccountBuildable, and in addition replace LoginRouter init, injecting CreateAccountBuildable.

Remember, we don’t use a concrete kind CreateAccountBuilder. Instead, we use a protocol CreateAccountBuildable

Now we will be able to entire pathToCreateAccount manner.

  1. We use createAccountBuilder to construct a createAccountRouter . We want to cross the present interactor as a listener in construct manner.
  2. We connect the createAccountRouter as a kid to the present router. This is how we construct s RIBs tree.
  3. We invoke LoginViewControllable technique to provide CreateAccount view controller.

First factor you’ll understand right here, is the next compiler error:

Argument kind ‘LoginInteractable’ does now not comply with anticipated kind ‘CreateAccountListener’

To repair it, we want to be certain LoginInteractable implements CreateAccountListener protocol:

Here is any other necessary factor to bear in mind. We use attachChild technique to connect createAccountRouter . Going ahead, you’ll sooner or later want any other technique to brush aside CreateAccount display. Once the kid display is brushed aside, we need to detach its router from the present tree.

We don’t need to see ourselves within the state, when the viewController is now not to be had, however the corresponding RIB continues to be within the tree. This might sooner or later purpose reminiscence leaks and surprising habits.

To steer clear of this, we can stay a connection with CreateAccountRouter . Create a variable inside of LoginRouter:

Now we replace pathToCreateAccount manner. We want to save createAccountRouter to a neighborhood variable. As an advantage, we will be able to guard ourselves towards making a router and presenting a kid view controller, if a kid router has already been created:

Finally, after we need to brush aside CreateAccount display, we need to detach its router once we manipulate with a view hierarchy:

Xcode will display any other compiler error, so we want to replace LoginBuilder and cross CreateAccountBuilder to router init. We use LoginBuilder to create and inject a kid builder:

Note, that we use part.dependency as createAccountBuilder dependency. To accomplish that, we’d like our LoginDependency to put into effect CreateAccountDependency protocol. This is how we attach dependencies from the guardian to kid RIBs:

CreateAccountDependency doesn’t have any variables in our instance. If it does, we can have to offer them in the future. It’s handy to create and stay the entire dependencies within the root part, after which cross them the usage of this protocol inheritance. We will do that via the top of this newsletter.

At this level, the app will have to collect with none mistakes.


LoginPresentableListener has excellent auto-generated documentation. We simply want to know what movements we need to carry out in this ViewController. We are going so as to add two how to LoginPresentableListener:

We aren’t going to concentrate on the UI, however you’ll be able to pass forward and create a easy UI if you wish to see in motion. Make positive the buttons cause the proper listener strategies.

Class LoginViewController implements LoginPresentable protocol, that we configured earlier than (so the interactor can be in contact to viewController). It way, LoginViewController has to put into effect showActivityIndicator manner:

The subsequent protocol the viewController implements is LoginViewControllable (so the router can adjust the view hierarchy). To conform, LoginViewController has to put into effect provide manner:

This is all we want to do in LoginViewController for now. Again, you’ll be able to upload lacking UI buttons, textual content fields, and the process indicator.

Because we added a couple of how to LoginPresentableListener , and LoginInteractor implements this protocol, we want to upload lacking how to interactor:

didTapCreateAccount has to path to CreateAccount RIB, so we simply want to invoke an present LoginRouting manner:

To invoke a login web-task we’d like get admission to to WebServicing login manner, that we created earlier than. We will cross WerServicing to LoginInteractor init:

Having a WebServicing in interactor, we will be able to entire the login manner:

Inside this system we put into effect the entire login industry common sense, appearing and hiding process indicator, brush aside LoginDrift on login good fortune, and log an error in case of login failure. Let’s additionally upload any other LoginPresentable manner showErrorAlert that may notify the consumer if login is failed:

The compiler will you’ll want to’ve applied this system in LoginViewController . Invoke this system from the login failure case:

Lastly, we need to replace LoginBuilder and cross WebServicing dependency into LoginInteractor:

Top-level RIB

Now we now have an entire Login module for our app. If you wish to have to look all of it in motion, it’s important to upload a couple of lacking items.

Create a Root RIB, that can be a guardian for Login RIB (you will have to have the ability to attach login to root the usage of the similar steps I equipped above. The few variations can be in RootRouter and RootBuilder, as it’s a top-level RIB that doesn’t have a guardian.

Instead of constructing RootRouting we want to create a LaunchRouting (particular RIB part designed for the top-level RIB):

This is an excessively particular case and it’s just about self-explanatory, so I’m really not going to enter main points.

RootRouter can even inherit from LaunchRouting as a substitute of ViewableRouter, this is launch-specific routing protocol:

We additionally want to create an AppComponent that makes use of an element with an EmptyDependency. This part could have lots of the dependencies we need to cross alongside the usage of Dependency protocol. You can create a WebService elegance that inherits from WebServicing protocol and stay it as a variable in AppComponent:

In AppDelegate we want to create a RootRouter the usage of this AppComponent , and lunch it from the present window:

At this level we will have to have the ability to collect and release the app. If you upload a lacking UI, you’ll be able to see it in motion.