Getting started with custom iOS frameworks — A DH Story

hasan aygünoğlu
6 min readAug 9, 2021

--

This was an assignment given to me by DonanımHaber (DH), the company I’ve been working at as an intern developer. They asked me to prepare a simple iOS framework that anyone on the internet can access/use through CocoaPods. I honestly had a pretty rough time coming up with a framework idea at first, but eventually I cracked the code and made a basic image filtering library. I called it BasicFilters (not very creative, I know).

As I understand it, larger-scaled projects in big companies usually consist of many different smaller modules. I believe this is called modular programming and it allows developers to execute/test only certain functionalities of a big project, while keeping the rest — and much bigger part — of it out of the equation. These smaller modules should be responsible of certain functionalities and they should be executable on their own. When put together, these modules — also called sub-programs — will construct a much larger application. I guess this whole concept is very similar to LEGOs, except, of course, this can get very complicated.

I honestly wasn’t expecting to write my own framework/library this early in my Swift journey. In all fairness though, the framework I made is relatively simple (as you’ll see below) and it didn’t really take that long to build it. However, after doing all the necessary research and coding the library itself, I do believe I built a fairly strong understanding of the overall concept of iOS frameworks and modular programming. I believe building this familiarity was the main reason why my superiors over at DH assigned me this task.

What I made

BasicFilters is a fairly basic image filtering library that allows you to apply simple filters to UIImage objects. It utilizes Apple’s own CoreImage framework. CoreImage already provides image filters but they can get fairly complicated to use. BasicFilters takes away all that complexity, throws it in the trash and makes image filtering a matter of a single line of code. Click here for the Github repository.

BasicFilters in it’s current version includes five different filters: Blur, Invert, Grayscale, Sepia and Bloom. Here’s an example demo project where I make use of BasicFilters to filter an image (yes, that is me in the photo).

How I made it

I’m gonna be honest. The idea of making a framework of my own seemed a little complicated at first. However, once I delved deep into it I quickly realized it’s actually a fairly simple process. Obviously frameworks can get super advanced, and this will surely translate into a much more complex developing process. But the framework I made as a beginner was quite simple and writing the code and publishing it through CocoaPods was definitely much easier than making a fully-fledged iOS application.

First step, fire up Xcode. Create a new project, and pick “Framework” from the “Frameworks & Library” section. After naming your project you’ll be greeted with this relatively barebones project directory:

What I did afterwards was to create a new Swift file inside my framework folder. First things I did was to import UIKit and CoreImage frameworks and then create the BasicFilters class. Here’s an example of how it looks:

The rest is basically just coding the functionalities of the framework. In my case, I was writing a simple image filtering library. So I started coding filter functions that will apply blur, bloom, sepia, grayscale or invert filters to an image. Here’s how my blur filter looks like:

CoreImage filters work with CIImage objects, which is why in the first line of my code I take a UIImage and turn it into a CIImage. Then I proceed to apply the CIGaussianBlur filter inside an if let block. Users can define the intensity of the blur by passing a double value to the function.

CIGaussianBlur usually gives you an image that’s bigger than the one you started with. That’s because pixels at the edges of the image get blurred out past them, resulting in an image that looks smaller on the screen. To fix this problem, we’ll have to make use of another CoreImage filter, namely “CICrop”.

Finally at the end of our code we turn our CIImage back to a UIImage, as our function’s return type is “UIImage?”, then return the filter-applied image back to the user. This whole process results in a pretty smooth way of applying a blur filter to an image, and with BasicFilters, anyone can utilize it using only a single line of code.

I followed a similar coding process for the other filter functions as well.

Releasing the framework on CocoaPods

CocoaPods was probably the easiest part of the project. These are the steps I followed:

  1. Create a Podspec file in your framework.
Creating a podspec file.

2. Open the Podspec file in Xcode an edit it. This is how my file looked like at the end:

Podspec file for BasicFilters.

3. Run ‘pod spec lint’ in the terminal. CocoaPods will go through your podspec file in a validation process. This is also where your pod actually becomes ready to use. Assuming, of course, the validation process ends successfully. Things might go wrong here if your framework does not include a LICENSE file. So make sure you have your LICENSE ready to go.

4. At this point, your pod is basically ready to use. However, it’s not available online, it’s only available to you. You can import it into your example project as a dependency by going through the usual ‘pod init’ and ‘pod install’ steps. Only this time, you’ll have to specify framework’s path in the podfile (again, it’s only available in your local drive).

5. To make our framework publicly available on CocoaPods, we’ll have to go through two more steps. First step includes a registration process. All you have to do is to run ‘pod trunk register…’ line in your terminal as seen below. You can later check your account info by running ‘pod trunk me’.

6. And now, for the final step, we’re going to actually publish our pod online. All we have to do is to run ‘pod trunk push BasicFilters.podspec’ in terminal. And assuming everything goes perfectly, we’ll be greeted with a beautiful success message. CocoaPods also sends an e-mail confirming the release of the pod.

How to use BasicFilters

  1. BasicFilters is available on CocoaPods. To install, add the following line to your podfile:
pod 'BasicFilters'

2. Import BasicFilters in your Swift file, and initialize it.

import BasicFilterslet basicFilters = BasicFilters()

3. Take a UIImage and pass it into one of the filter functions. An example usage inside DispatchQueue blocks:

--

--