Creating your own iOS framework and distributed using CocoaPods

iOS: 10

Language: Swift 3

Xcode Version: 8

Mac OS: 10.11

Creating your Framework

Step 1: Create simple Xcode project

  • For this demo, first create a Xcode project by selecting a template for our project Cocoa Touch Framework from iOS tab.image18
  • Click next and Give your framework name and save to your diskimage18

Step 2: Change the build configuration to release mode of your framework

  • Select your framework target and Navigate to Product->Scheme->Edit Schemeimage18
  • You can see the Build configuration is set to Debug. It means that whenever you run this project it will create our framework in Debug mode so if we want to use this framework in our application we switch to Release mode.image18
  • Now if we run this project it will be ready for release

Step 3: Add new swift file to your project

  • Now we want to add some functionality in our framework so we add a swift file to the project. Right click on project folder and select New File
  • Select Swift File from iOS tab and click next
  • Give the name of file and save to your project directory

Step 4: Create our first class in swift file

  • Create class named YLog. This can actually be used inside our framework project however we want but we want to use this class outside the framework so we have to mark this class as public.
  • Write following code to your YLog.swift file:
  • The purpose of this YLog class is going to be if our application is in Debug mode we want to printing out debugger but our application is in production for presenting to appStore we don’t want to print this debugger because we can’t see them and also slow down the application performance.
  1. Create a variable named isDebug so we manage application is in debug or in production mode.
  2. We should add initialiser to our class. So in init() method we initialise debug variable and set it to false because just incase if you forgot to set your app to debug mode, the Logging framework automatically stop printing debugger. We actually need this init() method to set as public so we can initialise our variable from outside the framework.
  3. The setup() method is used to set the application mode, set to true means application in Debug mode and false means in Production mode.
  4. Now we create actual logging method called YPrint and this is going to be a generic. In this method if isDebug is set to true, we print out the value in debugger otherwise not because out app in production mode and you can do any stuff in else part.

Step 5: Run your project and find your framework

  • Run your project by pressing Command+R
  • Now you can see the framework in products folder. Right click on it and select show in finder


  • Copy YLogging.framework file and paste it to your desktop.


  • Close your framework project.

Step 6: Create new simple Xcode project to use the framework

  • Create a Xcode project by selecting a template for our project Single View Application from iOS tab.


  • Click next and give name of project like FrameworkTest and save to your disk


Step 7: Add framework in our application

  • Create a folder called as Frameworks in your project directory and drag your framework from desktop into this folder
  • Now select your targets and click on + button under the Embedded Binaries tab.
  • Click On Add other and navigate to your project directory and select your framework from Frameworks folder

Step 8: Import your framework and Setup debugging mode

  • Open AppDelegate.swift file and at top write import YLogging.
  • Create global variable  so we can use this logging framework anywhere in our application.
    let logger = YLog()
  • Call the setup() method of framework from didFinishlaunchingWithOptions method and set isDebug to true.
    logger.setup(isDebug: true)
  • So Now your AppDelegate.swift file looks like below:image13

Step 9: Use of YPrint method

  • Open ViewController.swift file and in viewDidLoad() write following code:
    logger.YPrint(value: “Working fine…”)
  • Run your project and see the following output in debugger:
  • Now change the debug mode to false using setup() method from AppDelegate and see the output.
    logger.setup(isDebug: false)

Distributing your framework using CocoaPods

CocoaPods manages library dependencies for your Xcode projects. The dependencies for your projects are specified in a single text file called a Podfile. CocoaPods will resolve dependencies between libraries, fetch the resulting source code, then link it together in an Xcode workspace to build your project. Ultimately the goal is to improve discoverability of, and engagement in, third party open-source libraries by creating a more centralised ecosystem.

Here we are going to make our framework into a pod, so we are distributing the code, resolving the dependencies, including and building the framework source, and easily sharing it with our organization.

Step 1: Remove current framework from your project

  • Select YLogging.framework from the project navigator and delete it.
  • Choose Move To Trash to permanently remove framework from your project.

Step 2: Install CocoaPods

  • If you are not familiar with CocoaPods before, you’ll need to follow a brief installation process using CocoaPods Installation Guide before going any further.

Step 3: Create the Pod of your framework

  • Open terminal in YLogging project directory and run following command:
    pod spec create YLogging
  • This command creates the file YLogging.podspec in current directory. Right click on this file and open in text editor.
  • Remove all the text from that file and paste following lines  for commonly use settings for pod configuration:
  • Save the file and now you have a workable development Podspec.
  1. Specify the name of pod
  2. Version of pod
  3. Short description of your pod
  4. Point to a project page for the framework.
  5. Specify this iOS frameworks tutorial code using an MIT License
  6. Name of that person credited and contacted for this framework.
  7. Specify the platform which can use this framework.
  8. Setup the URL for link to the GitHub repository. When you’re ready to share the pod, replace URL with your GitHub repository url, so this will be a link to the GitHub repository and the commit tag for this version.
  9. Specify the source files folder and types of files which you want to add into pod.

Step 4: Use pod into your application

  • Now our pod is ready to use. Test it out by implementing in the FrameworkTest app.
  • Go to terminal and navigate to the FrameworkTest directory and run following command
    pod init
  • This will create a pod file into current directory. Open That file into text editor and add the following line under the comment, # Pods for FrameworkTest:
    pod ‘YLogging’, :path => ‘../YLogging’
  • Save this file and run following command in terminal:
    pod install
  • With this command, you’re searching the CocoaPods repository and downloading any new or updated pods that match the Podfile criteria. and it will create a FrameworkTest.xcworkspace file. Use this file from now on instead of the .xcodeproj file because it has the reference to the Pods project and the app project.
  • Open FrameworkTest.xcworkspace and run the project.

Step 5: Create GitHub Repository

  • Here you can publish your pod on GitHub and use like a third party framework.
  • If you don’t already have a GitHub account, create one.
  • Create a new repository to host the pod. Specify the name of repository here YLogging, you can name it whatever you want. Select Swift as .gitignore language and MIT as licence.
  • Click on Create Repository. From the dashboard page of your repository copy the HTTPS link.

Step 6: Add your framework to GitHub

  • Open your YLogging.podspec file from YLogging folder and update the s.source line. Replace URL with your repository link copied from GitHub dashboard
  • Go to terminal and navigate to YLogging folder and run following commands:
  • You need to tag the repository so it matches the Podspec. Run this command to set the tags:
  • Now your framework is on your GitHub repository. Go to the repository on browser and refresh to see changes:

Step 7: Update your podfile.

  • Update your podfile in the FrameworkTest directory. Replace the existing pod line with:
    pod ‘YLogging’, :git => ‘URL’, :tag => ‘1.0.0’
  • Replace URL with your GitHub Link.
  • Go to terminal and navigate to FrameworkTest directory and run following command to update your pod file:
    pod update
  • Now the code will be downloaded the framework from the GitHub repository.

Anup Kanjariya Jr. iOS Developer

I am a Jr. iOS developer at Yudiz Solutions Pvt. Ltd. - a leading iPhone App development company. I am interested in science and technology which encourages me to participate in inventing new things and concepts. But, my greatest love is programming, software architecture, writing code etc, in short, converting ideas into reality.

Comments are closed.