Creating and Integrating iOS Framework

vijay sn
5 min readAug 12, 2021

Introduction:

As a developer in general, We write a lot of code and generally create many things by ourselves over the years. Wouldn’t it be tiring to do the same thing again and again 🤔🧐

Yes, it is… We do it unconsciously that’s why we are not noticing at times.

Framework/Library is so useful in this case, where we can create things one time and consume it across the projects demanding the same behavioral functionality.

Pic Credits: School vector created by pch.vector — www.freepik.com/vectors/school

Frameworks are self-contained chunks of code where we can import into many apps encouraging reusability and reduce a whole lot of development time.

Frameworks have three major purposes:

  • Encapsulate code
  • Modularize code
  • Reuse code

Getting Started:

We are going to take a simple Regex validator which we mostly use for almost all the projects and we are going to see how it can be made as a framework.

However, instead of including the file directly for Regex Validation, we are going to include the same as a framework to have more control and avoiding the developers changing the source directly.

The main purpose of this blog is to encourage developers to make their own individual utility framework/library whenever new functionality is being developed. This saves our own time and also saves other’s time by sharing it with them.

Let’s Begin 🤘🏼

Creating a Framework

In Xcode, select File ▸ New ▸ Project…. Then choose iOS ▸ Framework & Library ▸ Framework.

Xcode-Project Creation Template

Click Next. Then set the Product Name to RegexValidator. Use your own Organization Name and Organization Identifier.

Xcode-Project Name Widget

Click Next. In the file chooser, choose to create the project. Then click Create.

Building the Framework

After the Framework is created, we are going to build the framework by adding the source code for RegexValidation (in this case).

I am just adding a new Swift File for Regex Validation since it doesn’t have a lot much to offer other than the Regex Validation.

Xcode-File Creation Template

By Clicking on Next, Give the file name and make sure it's added to the Framework target.

Finder-File Creation

After the file is created, adding source code to the RegexValidator File to perform the intended Regex Validation.

RegexValidation.swift

Adding the Files to Framework

If you have files already and needed to be included in the framework. Drag and drop the files into the framework.

Xcode-Project

Make sure to check Copy items if needed so the files copy into the new project instead of adding a reference. Frameworks need their own code, not references, to be independent.

Xcode — Options for adding files

Updating the Build Settings

To make the framework shippable and ready for distribution, We need to update the build settings of the framework accordingly.

Xcode-Build Settings

Open the Build Settings. Then set Build Libraries for Distribution to yes. This produces a module interface file that shows your public API when developers jump to the definition of your module in Xcode.

Upgrading the Access Control

Swift’s default access control is Internal which is accessible only within its own module.

Swift has five levels of access control. Use the following rules of thumb when creating your own frameworks:

  • Open and public: Accessible for other app or other frameworks.
  • Internal: Accessible within the framework.
  • Fileprivate: Accessible within the same file.
  • Private: Accessible within the class/function.

Since we want to access the Regex Validator Methods inside the framework from the application, we can make it public accessible.

Integrating the Framework

Go to the Consuming Project RegexValidator-Example and give File -> Add Files to “Project”.

Xcode — Add Files

We are adding the framework project as a subproject for making Framework testing and maintenance easy. We can skip this if we just want to integrate a centralized framework into multiple projects.

In the file chooser, navigate to the Framework folder and select RegexValidator.xcodeproj. Then click Add to add it as a sub-project.

Change Target to Framework Project and Build it.

Xcode — Build Succeeded

In the Sample Project, Frameworks, Libraries, and Embedded Content section — Drag framework from the Products folder of RegexValidator.xcodeproj onto this section.

Xcode — Adding the Framework

Now the framework is successfully added to the project.

Consuming the Framework

For consuming the framework, we should be importing the framework in View Controller or Class wherever we are making use of that functionality.

In this case, we would be importing the RegexValidator framework in the View Controller of the Sample project like below.

import RegexValidator

Once the framework is imported, we can make use of the Regex Validator methods directly. We are using Email Validation, for this example, to make things simple and easy.

On the button click, we are making the regex validation for the text in the text field. You can find how it functionally works with the help of the below screenshots.

Simulator — Valid Email
Simulator — Invalid Email

Easy… Isn’t it?… In this way, we can access a whole lot of features from the framework by adjusting the access control, and even there are options to override certain functions of the framework if access control is Open.

What’s Next?

Hope you would have enjoyed this story and understood framework creation and integration.

It’s always good to spend an extra 5 to 10 mins on top of your work to make the framework for the new functionality being developed.

The habit of creating the framework always saves time in the long run and helps us not to reinvent the same wheel again and again.

You can publish your framework as your own Swift package or Cocoapod and consume it directly in your project. Stay tuned for the upcoming blog regarding this topic.

Please free to go through the similar story I have written for Creating and Integrating Android Library — https://vijaysn02.medium.com/creating-and-integrating-android-library-77d570ab65a5

Check out this sample project that we have gone through this blog — https://github.com/vijaysn02/iOS-RegexValidator-Framework

--

--