Creating and publishing your own iOS library in Swift(2024) | Part 1

Prashant Dixit
7 min readApr 13, 2024

--

In this comprehensive beginner’s guide, we will embark on an exciting journey into the realm of iOS development. Get ready to create your very own iOS library using Swift and learn how to publish it so it can be used by other developers/organizations. So, without further ado, let’s dive into the benefits of building a library and start building one of our own.

What is an iOS Library & why should I make one as a developer?

In today’s fast-paced world of iOS development, it’s crucial to grasp the concept of creating your own iOS library. Why? Picture this: you’re working on an iOS app, and you want to incorporate some innovative features or functionalities. Instead of reinventing the wheel each time, you can streamline your workflow by utilizing pre-built modules known as libraries.

These libraries serve as pre-made components that seamlessly integrate into your app. By crafting your library, you expedite your development process and contribute to the broader developer community. It’s an excellent avenue to share your expertise and assist others in their app-building endeavors.

Now that we’ve outlined an iOS library and its benefits, let’s delve into the project setup and coding process.

Creating a custom iOS library in Swift

The first step in creating any project/library is to ensure you have a good and appropriate project structure based on the needs of your projects. Since we’re creating an iOS library which will be later deployed on Cocoapods, we need to find a project structure that suits the platform.

Before we start building the project, let me give you a brief overview on what Cocoapods is. CocoaPods is a handy tool for iOS and macOS developers. It makes managing external code libraries a breeze by automating tasks like downloading and integrating dependencies. With CocoaPods, developers can keep their projects organized and efficient with minimal effort.

Step 1: Setting up our project and building the starter project

Luckily Cocoapods provides a tool that is specifically designed to help you create a new CocoaPod library project with a structured directory layout and some boilerplate code. You can follow this link to learn more.

The command to create a new Cocoapods library project is as follows

pod lib create your_library_name
  • Open up the terminal in your desired folder, and paste the above command with your project name. I will be naming my project as ‘Snacky’ but you can use whatever name you want. It should look something like in the below screenshot.
  • Next, after running this command, you’ll see different setup options. Choose accordingly based on your project needs. Or just choose the ones that I have. We are creating an iOS library, which will be using Swift, for testing our library we would like to include a ‘demo project’ in our project, since this is a small sample project, we don’t require testing frameworks here.
  • Once you’re done with all the questions, your project will build and the boilerplate code will be ready for you to start the project. Your terminal should show something like this.
  • Now open your project in XCode, and you should see the following project structure. We have a main project folder Snacky and a Pods folder separately. Inside the Snacky folder, we have a Snacky.podspec file which will contain our library’s configurations, and an example project which we’ll use to test our library’s code.
  • Next, we have the Pods folder, where there is another folder named Snacky, it is this folder inside which we will write all the business logic for our library. It currently has a swift file named ReplaceMe. We will remove that and create a new Swift file named Snacky.
  • One last thing to set up before we begin coding is to ensure that both the example project and our library have the same iOS minimum deployment target version. Click on the Pods project (Pods.xcodeproj), then in the Info tab, you see the field Deployment Target, change the value of that field to iOS version 13.0 or later as per your need. But make sure that in the left-hand side Project section, Pods is selected. Here is the screenshot below.
  • We need to do the same thing in our Snacky Project (Snacky.xcodeproj). Click on the Info tab -> Select Snacky project-> Set Deployment Target value to iOS 13.0 or later.

Now if we run the project, it should build successfully and open on a simulator/real device. It took a lot of steps to set up this project, but we have a good project structure and code that builds successfully, so now we go to the exciting part which is writing our custom library code. Pat yourself on the back if you’ve reached so far. Let’s go coding now.

Step 2: Adding code inside our library

  • Open the Snacky.swift file we created above in Pods->Development Pods -> Snacky folder. We will create a custom iOS alert function that accepts parameters such as title, message, alert style, and color. This function will display a customized UIAlert to the user. By using this function, we can avoid repeatedly writing the same code to display alerts. We pass the parameters as values while we’re initializing our alertController and returning the modified alertContoller for a user to display on their screen.
import Foundation

public func createAlert(title: String, message: String, alertStyle: UIAlertController.Style?, color: UIColor?) -> UIAlertController {
let alertController = UIAlertController(title: title, message: message, preferredStyle: alertStyle ?? .alert)
alertController.view.subviews.first?.subviews.first?.subviews.first?.backgroundColor = color

return alertController
}

Congratulations, pat yourself on the back. You just created your first library, well done!! Now that our library code is ready and before we deploy it, we need to test our library locally to ensure our library runs perfectly.

Protip: Make it a habit to test each new version of your library when it is included in a sample project so that you can see how the changes affect the app and how simple it will be for developers to use your library. This is a no-brainer, but it will help you avoid making mistakes later on.

Step 3: Testing our library locally

  • Next, we go to our ViewController.swift file in our Example project, the path for which is Snacky -> Example for Snacky -> ViewController.swift.In this file, we’ll build a button, which when pressed will call our library’s createAlert function and display an alert. First, we import our library to this file to access Snacky’s functions.
import Snacky

Next, we write code to programmatically create a button and implement its onPressed function which will call the Snacky.createAlert(…), and then present the returned alert to the user on the current screen.

    override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let button = UIButton(type: .system)
button.setTitle("Display Alert", for: .normal)
button.setTitleColor(.white, for: .normal)
button.frame = CGRect(x: 100, y: 400, width: 200, height: 50)
button.backgroundColor = .blue
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
view.addSubview(button)
}
    @objc func buttonTapped() {
let alertController = Snacky.createAlert(title: "Hello",
message: "Publishing library in iOS is fun!!",
alertStyle: .alert,
color: .gray)

present(alertController, animated: true)
}

Now you can see how many customizations we can make to our Alert thanks to this function, you can play around with different configurations and properties of UIAlertController but that is beyond the scope of this article. Here is the full code for ViewController.swift file

import UIKit
import Snacky

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let button = UIButton(type: .system)
button.setTitle("Display Alert", for: .normal)
button.setTitleColor(.white, for: .normal)
button.frame = CGRect(x: 100, y: 400, width: 200, height: 50)
button.backgroundColor = .blue
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
view.addSubview(button)
}

@objc func buttonTapped() {
let alertController = Snacky.createAlert(title: "Hello",
message: "Publishing library in iOS is fun!!",
alertStyle: .alert,
color: .gray)

present(alertController, animated: true)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}

Step 5: Build and Test our sample app

Let’s build our sample app now and test whether our library is working. As you can see on clicking the button we see a UIAlert on our screen with all the custom values that we passed in our createAlert function.

Step 6: Conclusion & Future Steps

Wohooooo Enjoy🎉🎊🥳🥳.

In this tutorial, we learned how to create a custom library add it to your local project and test it. In the next part of this series, we will be working on publishing our iOS library to Cocoapods and SPM.

You can find the source code of this project on my GitHub. Here is the GitHub repository link.

Thanks a lot for reading this article and follow me for upcoming articles related to app development. Please give a clap if found this article useful. Happy learning😊.

--

--