Getting Started With the iOS Feature Flag (Objective-C)

Here’s another post I wrote for Rollout, this time about integrating their feature toggle system with an iOS app written in Objective-C. You can find the original post here.

 

Mobile apps are often tied to backend services and are expected to evolve in parallel with them.

Of course, the backend developers have it a lot easier. They can release a new version of their application pretty much anytime they wish. For many microservice frameworks, the ability to quickly push new API versions is a core feature. It’s expected.

iOS developers aren’t this fortunate. Regardless of how quickly they can turn a release around, everything needs to go through the App Store and users need to update the application. Both of these factors are a constant drag on the release cycle.

How can we work around these obstacles? With feature toggles.

Feature toggles (also known as feature flags) give us a mechanism for adding new behavior without deploying a new release. We can even enable or disable the new functionality when we wish.

So we add a new configuration parameter to our application, set it to “off,” and then flip it on when we’re ready, right? Well, there’s more to it than that. Using feature flags to effectively manage change and improve your user’s experience requires understanding feature flag strategies and tactics.

In this tutorial, we’ll add a feature toggle to a simple iOS app, first as a configuration parameter and then as a managed flag that will demonstrate how to avoid creating technical debt while adding the ability for your application to evolve quickly.

This tutorial assumes familiarity with basic iOS concepts and the ability to set up and use Xcode. The source code for the complete project is available here.

A Basic Feature Toggle

We’ll start with the single view iOS application offered in Xcode.

 

select xcode project for feature flags

Then we’ll add a UILabel to the application’s single view.

Add label to Swift feature toggle application

Next, we need to define the label in ViewController.h with the name greetingLabel.

Finally, we’ll set a value for the label in ViewController.m. Add the assignment in viewDidLoad.

Of course, setting the default value in the UILabel definition is another way to do this, but we’ll define it explicitly throughout the tutorial to make the behavior easy to see.

We’re done setting up the sample application. Let’s build it and run.

Test Swift iOS application

 

Now we can use a feature toggle to modify this application’s greeting.

An iOS Feature Toggle

Let’s imagine we want to replace our greeting with a different one depending on the season. The least common denominator is a boolean flag that toggles one string or another.

So let’s add some logic to how we set the greeting.

Now build and run this version of our application.

Feature toggle in Swift iOS application

We have our first feature toggle!

But this means modifying, building, and getting a release through the App Store whenever we want to change our greeting. It’s also a potential form of technical debt. What happens to these boolean flags down the road? Do they stay in the code, all set to true?

Using a configuration file would at least shave a little bit of time and effort off the release cycle.  We’ll create a property list and control our toggle with a configuration property.

Click your project’s name and then click File|New in the main menu.

 

feature flag objectiv-c add property file

Click on property list and then Next.

feature toggle objective-c name property list

Name the file as shown and click Create.

Next, add a property name isNewYear.

feature flag set property

 

 

We now have a property list named featureflag.plist. In it, there is one parameter: a boolean flag named isNewYear.

We’ll load it and use the property to set our flag. Rather than encapsulate the configuration code in another object or the AppDelegate, we’ll load it right where we use the feature to keep things simple.

Now we can toggle the value of isNewYear to YES or NO via the properties file and we’ll see the proper greeting.

The plist gets us out of modifying code when we want to toggle a feature, but we still need to build and release for each change. We’ll also accumulate debt in the form of unused configuration properties unless we remember to remove them in future releases.

Let’s look at a better way to change—and more importantly—manage feature toggles.

Getting Started with Rollout

First, before you can follow along, you’ll need to create a free Rollout account here.

After you’ve finished registering, sign in to your Rollout account and create a new application.

Rollout new Feature Toggle iOS Application
Be sure to select iOS, as shown above.

Next, you’ll see instructions for adding Rollout to your application.

Install feature toggle api iOSFirst, download rox.zip, unzip it and add the rox directory to your project’s directory tree.

Next, add RoxSdk.xcodeproj and ROXCore.framework to your project.

Then select Add Files to… from the project navigator.

add files to projectNow select the files from the RoxSdk directory in the zip file output.

select rollout files

 

You’ll see ROXCore.framework icon in the project navigator. Add those files.

Next, we need to remove the framework from the project’s linked frameworks. Select your project’s name at the top of navigator tab.

Select feature flag project

 

 

Scroll down in the application information page to Linked Frameworks and Libraries.

Xcode linked frameworks

 

 

Next, select ROXCore.framework and then click the minus to remove it.

We want to add it back to embedded binaries. Click the plus in Embedded Binaries.

embedded binaries

Then select ROXCore.framework and ROX.frameworkiOS and click Add.

Last, we need to set up a build step to strip out unneeded libraries from our production application.

First, add a script build phase.

Add script

Then locate the strip_frameworks.sh script.

Locate script

And drag it into the script target.

Drag in script

Initialize the API

The Xcode project is ready now and we can initialize the API.

Finally, click Setup SDK back at the Rollout dashboard.

Setup Feature Toggle SDK

The instructions in this dialog are for Swift. We’ll need to make some adjustments for Objective-C.

First, include the import statement for the RoxCore.h at the top of AppDelegate.m.

Then add the call to ROX setupWithKey to didFinishLaunchingWithOptions. Pass it the unique initialization key Rollout assigned to our application.

The top of AppDelegate.m should look similar to this:

Now we can run the application to initialize Rollout. Click Activate App on the Rollout dashboard. After the application builds and runs, you are rewarded with this:

Feature flag success

Add a Managed Feature Flag

Now we can add a managed feature toggle. If we look at our Rollout dashboard and check flags, we see this:
Create Spring Feature Toggle image
There are no flags and there is no way to add one. That’s because we create them from our application, not from the dashboard.

In Objective-C, feature flags are ROXFlags and are managed in RoxBaseContainers.

First, add a header named MyContainer.h to the project.

Then add the corresponding implementation.

This gives us a single flag named isNewYear with a default value of false. It’s managed in MyContainer, which we need to register with Rollout.

We register the container with the API before calling ROX setupWithKey.

Modify AppDelegate with the calls to create and register the container.

We pass two arguments in the message: a name for the container and an instance.

Run the application and then go to the Flags menu item in the Rollout dashboard.

Feature Flag in Rollout DashboardThe new flag is there. Both the container name and the flag are on the dashboard.

Reading a Managed Feature Flag

Next, we’ll update the application to use the RoxFlag instead of a property from a property list.

ROX retrieves values asynchronously and caches them. On startup, it extracts the latest value from the cache and initiates a fetch from the network in parallel. It will also refresh the cache when the application regains focus.

This means that the first time we run our application, we will immediately get the default value for our flag (false for toggles unless we override) and then the value set at Rollout upstream will be retrieved later.

We want to see the value of the flag before we load our view, so our label is up-to-date when the application opens.

We’re going to create a configuration object for ROX and pass it to ROX setupWithKey.

First, we create a RoxOptions. This holds configuration settings that we pass to setupWithkey.

One of the possible options is a callback message that ROX sends when the latest configuration is fetched: onConfigurationFetched. It is passed a ROXFetcherResult that indicates whether or not the fetch was successful.  In this example, we are assuming it was successful.

When ROX sends the message, we use NSNotificationCenter to notify any interested objects that new information has been retrieved.

There are a few advantages to using NotificationCenter instead of trying to call our view directly:

  • Since this callback will be called on startup, our ViewController probably hasn’t been created yet.
  • If we add more controllers to the application, later things will start to get complicated.
  • Threading issues are delegated to listeners (as we’ll see).

Next, we’ll modify ViewController to process the update message and set the label.

ViewDidLoad is now subscribing to update messages and delegating them to a method named updateLabels.

We moved control of our label to updateLabels, where we get a reference to AppDelegate to access its copy of MyContainer and use it. However, we have to dispatch this task to the main thread because only the UI thread can modify the view.

Finally, isNewYear provides us with a block we use to set the label appropriately.

Managing a Feature Flag

We manage flags by adding them to experiments. An experiment is a set of criteria for controlling flags (and other variables) in production.

First, click on Production in the left-hand side menu and then click Experiments. This will bring up a screen with a Create Experiment button. Click that and fill out the new experiment window appropriately.


Select Set Audience.

 

And we see a console for setting flags to true, false, or split. Leave the flag set to False for now.

Run the application and you see “Hello, World!”

Next, change the flag to True on the dashboard and restart the application.

Feature toggle in Swift iOS application

We see the Happy New Year greeting!

Flip it back to False and instead of stopping the app, click the phone’s home button.

Then tap on the application icon to open it again. The label now says Hello World!

We can change the behavior of the application without touching code and releasing a new version. We can even alter behavior while the application is running on a client’s device.

Before we move on, let’s take a look at the experiment on the console again. Flip the flag from False to Split.

Split Settings for a Feature Toggle

This option is where the term “experiment” comes from. We can distribute a flag’s value to a percentage of users. It’s a mechanism for implementing A/B testing in your application.

This is the power of feature flags!

Extra Credit: Configurations

Tying the contents of a label to a true/false flag isn’t what we would do in a production application but it kept the introduction to a boolean flag focused and simple.

Let’s take a look at a better mechanism for setting a label. Rollout supports managing configuration values from the dashboard.

Let’s add a new field to MyContainer.

First, add a property to the MyContainer’s header file.

Then initialize it in the implementation file.

RoxConfigurationString is precisely what it sounds like. It’s a string that is controlled by Rox.

Build and run the application again and then go back to the Rollout dashboard.

Under Production and Configurations, we see our new value.

Feture toggle configuration

If we click on it, we can set it to a value like so:

set feature toggle value

 

Select >= for the criteria and enter “Happy New Year!” (without quotes).

Then we need to make a small code change. Change updateLabels to use the value of our new property.

And run the app. We see Happy New Year!

Let’s change it.

Set Feature Toggle to w00t

Close and open the application. No need to restart or rebuild: our notification mechanism will handle the change.

Feature toggle developers say w00t!

We can change our application config with Rollout!

Conclusion

This guide demonstrates how to get started with Rollout with iOS and Objective-C. The API supports any iOS platform that runs Objective-C, including Apple TV. Rollout’s documentation has more information about how you can do a great deal more with flags, configuration settings, experiments, versions, and grouping users.

We’ve written a simple application that demonstrates how feature flag management can help you manage your projects and eliminate unnecessary releases. Try it out on your own!

 

Photo credit:  Liam Desic

Leave a Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Proudly powered by WordPress | Theme Name: BlogEasy by ThemesPoint.
%d bloggers like this: