How to create a binary iOS framework in swift

Sometimes, you want to share your code with the world but don’t want them to access your code. Well, if you are thinking the same, this article is for you. We can create an iOS universal binary framework using swift and share framework file. This might be helpful if you are working somewhere where generating framework is important. Let’s dive into it.

Before starting you need to know armv7, arm64, i386 and x68_64. They are basically architectures where starting with the arm is physical iOS device architectures and remaining two are virtual iOS simulator architectures.

Ok, let’s start now.👨‍💻

Create a Project

Open Xcode and create a new project(⇧+⌘+N) for your brand new framework 😉.

Select Cocoa Touch Framework and continue with other detail as you need. I guess you can do that easily. Let’s assume the project name “SMPrint”

Once the project is created, delete SMPrint.h file as we don’t need that.

 

Develop Functionality

Now let’s create our framework to expose some APIs to the world.

Under SMPrint folder within your project create a swift file called SMPrint.swift. 

import Foundation

public class SMPrint {  
    public class func printAnything() {
        print("Ok, printing anything...");
    }
}

Don’t forget to label public otherwise, the user cannot access your methods.

Well, that’s all to create an awesome framework. But we need is .framework file and that is exactlywhat now we going to do now.

 

Generating Framework

Generating framework is so easy. Build (⌘+B) your project first targeting to simulator and next targeting to a real device. Once you are done, right-click the SMPrint.framework file which is available in Products folder and click Show in Folder.

And there you go, you can find SMPrint.framework file ready for simulator only. Wait, then what about the real device? Well, in the framework directory go back and you can see Debug-iphoneos folder which contains framework for real device.

Test your framework

Create new Xcode project and import the framework in your project.

Now in your ViewController.swift import SMPrint and call method printAnything.

SMPrint.printAnything()

👍Done.

This way you can send share your work to the world hiding confidential logic. I have shared this article with you so that you could at least know how the framework is generated. Of course, this article is not enough as you might have already felt that we need a framework that works for both simulator and real device. I will cover that in next article just to make this article small and simple. In next article, I will share you how to create a universal framework and how to publish it using CocoaPods.

Let me know if you have any confusion and I will try to clear it out.

Thanks for reading.

😎Happy Coding😎

About the Author

Leave a Reply

santoshm