How to create an Umbrella Framework in Swift?

Using Xcode 9.2 and Swift 4

Although Apple discourage creating umbrella framework, there are situations where developer is asked or required to create the same.

So what is an umbrella framework? Why is it used?

Umbrella frameworks are frameworks which contain frameworks within. This can be used in several situations. When you are developing a custom network class which require a different parsing logic or need to implement a custom online payment functionality, you might create an umbrella framework.

Here, we will start with creating a simple framework and we add this framework to another framework making it umbrella framework. We will then create a sample project to incorporate umbrella framework.

Creating sub framework

1 Select Cocoa Touch Framework and give it a name. Here I am giving name as SubFramework

Image for post
Image for post

2 Create an Aggregate Target for this framework, so that we can make this as Universal Framework.

Image for post
Image for post

3 From the Aggregate target go to Build Phases and create New Run Script Phase

Image for post
Image for post

4 Add this script to Run script so that it can build a Universal Framework with full bitcode enabled.

5 Go to Build Settings from Aggregate Target and add -fembed-bitcode to Other C Flags. If you don’t give this flag, you might not be able to archive the app with this framework.

6 Create a New Swift file and write your own operations. Here, I have created file named SubFrameworkHome and added methods as below. Make sure your class is open , so that Umbrella Framework can access it.

7 Now select Aggregate Target from the build panel and build. You will be seeing .framework being generated from the run script. Copy the framework and keep it ready for use of Umbrella Framework.

Now that we got sub framework ready, let’s create Umbrella Framework.

8 Same as we did for SubFramework , Create new Cocoa Touch Framework and add Aggregate target to same. I will call this framework as UmbrellaFramework

9 Repeat steps 3 4 and 5

10 Copy SubFramework.framework to root folder of Umbrella Framework project.

Image for post
Image for post

11 Create a New Copy Files Phase in Aggregate Target → Build Phases and select SubFramework.framework from root folder. Make sure you add Destination as Frameworks

Image for post
Image for post

12 Select Framework Target and add SubFramework.framework in Link Binary with Libraries and Copy Bundle Resources. Create a New Copy Files Phase and add the same to list. Make sure you enable on Code Sign on Copy

Image for post
Image for post

13 Create a New Swift file and we will call methods from subframework there. I created a class like below.

I made both class and method as open so that App can invoke Umbrella Framework functions.

14 Now select Aggregate Target in build panel and build.
You will be seeing UmbrellaFramework.framework got generated in root folder.

There you go. This is Umbrella Framework.

Now that we are done with Umbrella Framework, we need to implement the same in an App.

15 Create a New Single View App. Copy UmbrellaFramework.framework to root folder of App.

16 Click on App Target → Embedded Binaries → Add the framework to it. It will automatically be placed in Linked Frameworks and Libraries

Image for post
Image for post

17 Now go to Build Settings → Framework Search Path → Drag and drop the Framework to it. You should also explicity mention the Frameworks inside the Umbrella framework. See below for reference.

Image for post
Image for post

18 Do the same for Project Settings.

19 Now App target → Build Phases → Create a New Run Script Phase and add this script to it.

This script is done to make sure the Code Signing is done in subframeworks too. If not given you might face certificate issues in archiving the app with Umbrella framework.

20 You can import Umbrella framework to your required Viewcontroller. The one which I created look like this:

Here the result on launching ViewController is:

Please find the github link for this complete article here:
https://github.com/bittudavis/UmbrellaFramework

Got queries? Comment below.

Written by

Read more on www.bittudavis.com

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