Create framework in IOS using UIKit/ StoryBoard

Hi Guys,

In this post, am going to tell you about Framework in IOS. What is Framework? Where we can use it? and How we can create it. & How we can use it?

Don’t waste time on other things let’s dive into it.

What is Framework?

Every day we use many libraries in our project to reduce our efforts or to build something fast and effective, for that we write code that everyone can use(reused code), and reduce their efforts. In IOS we can create a library that is Framework in IOS.

For Example, we have an app that needs to show some webpage, with download file functionality from the webpage to an IOS device.

For that, everyone has to do lots of things in their project to implement functionality. For that, we can create a framework and share it with everyone, so that they can start using it. Without adding extra effort.

Where we can use it?

We can use the framework everywhere(Only in IOS). Whenever we feel that this code is used by other developers. In that case, we can create a Framework and use it in the project.

Now, the main question is, How?, How can we create a Framework?

You guys will find lots of tutorials on this. How we can create a Framework?

That framework implementation is simple create a function give a public modifier and use it.

In this tutorial I will explain you How to create separate screen in Framework using Storyboard UIkit and share with other team members and how they can implement it in there project.

Without any delay let’s start diving into it.

First, we need to create a Framework project

Goto File –> New –> Project then Select the Framework option from the Framework & Library Section as shows below in the following image:-

Then give a name to your framework and select the location where you will save the project in your system.

Now the framework template is ready. The framework template looks like same as mentioned below:-

Now we need a Product folder here. For that, we need to add a framework in our framework to add the Product folder.

Please follow the following steps to achieve it:-

Goto:- Target of Framework –> In General Section –> Press + button in Framework & Library –> Add any Library from that.

When you press the Add button this will create the Products folder in Project Navigator of Xcode.

Now remove the added Framework. The step we did for creating the Products folder and in that framework is automatically created using that step.

Now we need to create a Storyboard and do code, why do you need a framework?

In my case, I am creating a Framework for open weburl in our framework.

Now we need to create a Swift file “MyViewWebView” where we can add our WKwebView in our view.

Add the below code into your MyViewWebView.swift file.

Now we need to link our Storyboard with the Swift class we created above.

Open Storyboard and select your class name in the Custom Class section, with this your Storyboard is connected to your Swift file. And add the Storyboard ID as same as your storyboard name.

Almost our framework is ready once you have done all your code in the storyboard;

Now we need to create 1 more swift file where we can create a public reference of your storyboard to another app so that they can call our storyboard from their app.

I am creating a swift file with the same name as our StoryBoard name as follows:-

Add the below code into it.

Using this code IOS app will be able to open our storyboard from their NavigatorController.

Now we need to create XCFramework for our code so that other developers can import and use it. That is why we created this framework right?

We need to run 3 commands, to get the final result.

Open Terminal and enter it into your project folder.

Note

Please replace MyFramework name with your Framework name

This command will create an archive of your framework that will work on an IOS simulator

$ xcodebuild archive -scheme MyFramework -configuration Release -destination ‘generic/platform=iOS Simulator’ -archivePath ‘./build/MyFramework.framework-iphonesimulator.xcarchive’ SKIP_INSTALL=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES

This command will create an archive of your framework that will work on IOS

$ xcodebuild archive -scheme MyFramework -configuration Release -destination ‘generic/platform=iOS’ -archivePath ‘./build/MyFramework.framework-iphoneos.xcarchive’ SKIP_INSTALL=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES

The following command will merge both archives into a single XCFramework

$ xcodebuild -create-xcframework -framework ‘./build/MyFramework.framework-iphonesimulator.xcarchive/Products/Library/Frameworks/MyFramework.framework’ -framework ‘./build/MyFramework.framework-iphoneos.xcarchive/Products/Library/Frameworks/MyFramework.framework’ -output ‘./build/MyFramework.xcframework’

Output will be like in your project -> build ->

Yeah…., we created our First framework, We can now share selected folders with anyone so that they can use them.

How to use a framework?

First, we need to add a framework to our project. Drag and drop the framework into your project

Now the next step is to Embed and Sign it from the General section of Tragets as below:-

Use this command to build project CMD+b.

Then in your ViewController import your framework, in my case I import MyFramework and add code to open the Framework storyboard from their app as below:-

https://gist.github.com/shivams1110/3a5db6b32262cd24600b1a0e8df800c8

Run the project and you will get what you want, Open the framework screen from your app.

Output:-

This is the App screen with the button, When the user press this button, it will open the Framework screen.

This is the Framework screen.

I hope this will help you, How to create a framework in IOS using UIkit or Storyboard. How we can use it in our project as well.

If anyone wants to give me any advice, improvement suggestions are always welcome.

Thanks, Regards &

Namaste