How To Implement a Great Onboarding Architecture in Your App

5 min read#iOS#Architecture#Onboarding

Photo by Jon Tyson on Unsplash

What Is Onboarding?

App onboarding is the phase when the user first engages with your app, learns how to use it, and does the first setup so they can start enjoying your creation.

For us developers, this is not that trivial — we’re used to architecture when every screen decides what’s the next screen to open. In onboarding, the logic of what’s on the next screen has nothing to do with the current screen. That’s what makes it a different creature in our project.


What Do We Want From Our Onboarding Process?

Of course, we can just push a new view controller whenever the user taps the next button, but with this method, we might encounter several issues:

  1. Each screen must know about the next screen, and it has to feel like a linked list. What if we want to change the order of the screens? What if a new developer wants to understand the flow of the onboarding? Do they have to go VC by VC and look for the navigationController?.push command? So, what we want is for every component in the process to be fully independent, without being connected to any other component.
  2. Following the previous point, we want to be able to change the order of the components. We might want to A/B Test it or just see whether a different order feels better for us.
  3. Sometimes, there are stages in the process that aren’t necessarily relevant for certain users, so we wish to insert some logic here. Sometimes, we want to skip certain components or add components for different users.
  4. Since it’s a sensitive part of the app and we’re also amazing developers, we want to write unit tests for the whole process.
  5. We don’t want to spread our logic in different classes of the feature. Instead, we want to funnel it in one class to manage the whole logic.
  6. We may want to track the user process. For example, maybe we want to restore the state in case the user killed the app in the middle of the process. That way, they won’t need to start from the onboarding process from the beginning again.

Create an Onboarding Coordinator

Note: This is just a suggestion on how to implement a decent solution to the problems mentioned above.

First, we need to create a singleton to manage your onboarding. You can call it a coordinator, router, manager, service — it doesn’t matter. The point is that this coordinator will be the only object that’s aware of the order of the components. It will also be the only part of your app that holds the logic of what to show next.

The coordinator can have a reference for the navigation controller so it can push the next component when needed.

It’s best practice to create an enum to describe the stages of the onboarding:

enum OnboardingStage {
    case welcome
    case purchase
    case photosPermissions
    case pushPermissions
    case tutorial
    case letsStart
}

The onboarding coordinator needs to hold two values — the list of the stages (actually, the list of the enums) and a pointer that represents the current user position in the onboarding. This value can be persistent for a later restore.

enum OnboardingStage : Int {
    case welcome
    case purchase
    case photosPermissions
    case pushPermissions
    case tutorial
    case letsStart
}

class OnboardingFlowCoordinator: NSObject {

    let currentStageKey = "currentStageKey"
    
    var currentStage : OnboardingStage {
        set {
            UserDefaults.standard.set(newValue.rawValue, forKey: currentStageKey)
        }
        
        get {
            if let stage = OnboardingStage(rawValue: UserDefaults.standard.integer(forKey: currentStageKey)) {
                return stage
            }
            return .welcome
        }
    }
    
    let stages : [OnboardingStage] = [.welcome ,.purchase ,.photosPermissions ,.pushPermissions ,.tutorial ,.letsStart]
}

Create a Protocol to Communicate Between the Coordinator and the Components

When the onboarding starts, the coordinator can decide to allocate and push a new component to the stack:

    func pushStage(stage : OnboardingStage) {
        switch stage {
        case .welcome:
            // push welcome screen
        case .purchase:
            // push purchase screen
        case .photosPermissions
            // push photos permissions screen
        case .pushPermissions:
            // push "push notifications" screen
        case .tutorial:
            // push tutorial screen
        case .letsStart:
            // push "let's start' screen'
        }
    }

When the current component finishes, it informs the coordinator. Then, the coordinator does its logic, move the current user position to the next stage, and pushes the next view controller to the screen according to the current user position.

    func finishStage() {
        moveToNextStage()
        pushStage(stage: currentStage)
    }
    
    private func moveToNextStage() {
        // move "currentStage" variable to the next stage in the stages array
    }

Better Support for A/B Testing

First, a few words about what A/B Testing is. A/B testing is a randomized experiment with several variants of user experience. During this testing process, you can compare your app performance in terms of revenues, retention, or any other metric of your choice. That way, you can optimize your product for the best results.

When dealing with onboarding, A/B Testing can crucial. Without a good coordinator, it will be almost impossible to implement it.

For example, let’s say that we have a photos app and our onboarding process contains several components, including permissions to photos library, a tutorial, a push notification permission screen, and an in-app purchase screen.

We’re not sure whether the in-app purchase screen should be the first screen or the last one, so we create two groups. Group A sees the purchase screen first, and Group B sees the purchase screen last.

    func currentStagesArray()->[OnboardingStage] {
        if group == "a" {
            return [.welcome ,.purchase ,.photosPermissions ,.pushPermissions ,.tutorial ,.letsStart]
        } else {
            return [.welcome, .photosPermissions ,.pushPermissions ,.tutorial ,.letsStart,.purchase]
        }
    }

With only four lines of code, we can now support two different variants of the onboarding process. It’s another thing we almost get for free when we have something that manages the process.


Writing Unit Tests

So, the coordinator can also help us write unit tests! All we need to do is inject the list of components, which is called the nextStage method, and see the results.

Now that we have the logic in one place, we can mock all the data we need and test it. There’s no need to touch the view controller in our tests.


Conclusion

Onboarding is a very important part of your app. Without a router/coordinator/service, it will be impossible to optimize, test, and maintain your app over time. Using the principles mentioned above can give you the foundation for that.


Originally published on Medium.

Be the first to like it

Comments

Leave a comment

Comments appear after they're approved.

All posts