Kabar Informatika
Published in

Kabar Informatika

Simple MVP architecture for iOS App

how to create a simple MVP architecture with unit testing for iOS Application

source https://www.architecturaldigest.com/architecture-design

Define the functional requirements are important for developing an application. It is consist list of user story that describe interaction between user and application. The user story can written by using this pattern : “as a <role>, i can <verb> <noun>”.

I will show you the implementation of Model View Presenter (MVP) architecture for an iOS application by using simple scenario. The application is a registration page. As a user, i can register my self by using email, password, full name and phone number.

The single sentence of functional requirement above is look simple. But the real user may have various behavior. Maybe he did not write down the phone number. User maybe typing the password less than 5 character which your application only permitted more than 8 char password length. For accommodate all combination of user’s behavior lets make a testing scenario.

  1. users register by filling in all field.
    expectation : registration succeed
  2. users register but the email’s field not filled.
    expectation : registration failed with message “the email can’t be blank”
  3. users register but the password’s field not filled.
    expectation : registration failed with message “the password can’t be blank”
  4. users register but the password’s field contain less than 8 character.
    expectation : registration failed with message “the minimum password length is 8 character”
  5. users register but full-name’s field not filled.
    expectation : registration failed with message “the full name can’t be blank”
  6. users register but phone-number’s field not filled.
    expectation : registration failed with message “the phone number can’t be blank”

The MVP basic concept.

The reason of using mvp-arch is to make our code base testable. According to the concept of clean architecture we should make strict boundary between the business logic and view logic. So we can test the business logic part and view logic part separately. Therefor it isn’t good if we put anything logic on UIViewController.

View is a part which responsible to control the state of view’s component like Button, UITextField, UITableView, UIImageView. It is consist of command to enable or disable the button or command to show or hide the UIImageView. Besides, it also be liable manage the state of UIViewController’s lifecycle. The other part is presenter which responsible to handle the business logic. It is may contain a set of rule to show or hide the UIImageView. For me the Model is a part which just only contain plain swift object. It is responsible to keep the state of some related object.

Lets make a Proof of concept by showing a registration’s page on application.

#1. Build a new project.

I just create a simple application with single page. It is contain four UITextField and one Button which will be controlled according to business rule.

#2. write the unit testing.

Lets start by creating an initial unit test. This is derived from test scenario which we discussed before. For a moment, I put XCTAssertFail() to each test function until i make the real implementation.

All of unit test function above are business rule. So we need to create an empty class to write down the business logic. As mentioned before, a business logic should be handled in Presenter part, so we have to initiate a new class called RegistrationPagePresenter.

Let see the MockUIViewController1 part. It is represent of UIViewController on real project. It doesn’t know the logic of checking empty email. The UIViewController just access the command register(…) and then it receive a response from RegistrationPresenter via RegistrationDelegate.

#3. make the implementation

Now lets create the implementation of RegistrationPresenter for checking whether an email was empty or not. We should write down the other business logic in here to validate the registration process.

For now we can run the Unite Testing by click Product > Test on menu bar. Let see what happen. Don’t forget to complete the implementation of business rule on registration process. You can check my full source code on Github.

#4. running the apps

Its time to launch the application and test it manually by filling all registration data manually. Do you see how clean the code is bellow?

Finally we reach to the end of tutorial. For me the MVP architecture is good enough to make our code base better. It is easy to learn, easy to test and easy to maintain.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store