Creating your own & simple Framework in iOS

sridivya

Creation of framework will benefit a lot in a distribution environment. Frameworks provide you some common set of features that will be needed in every application. In developing a software, framework reduces time and energy.

Through the use of frameworks development becomes a lot easier, So if it’s easier it’s faster and leading to efficient.A widely used framework have big security implementations, So users will use for long term.

Creating of our own and Simple framework is easy and will be explained below by steps:

The logic of the app is very simple:

1.Creating a method in a framework that generates the random RGB color.
2.An application uses the random color generated by framework and displays that color to view background.

A framework can be imported into any number of apps and even share across iOS, tvOS, watchOS, and macOS apps.

Mobile App

Getting Started:

iOS:10
Language: Swift 3
Xcode version:8.2.1
Mac OS: 10.12.3

Step 1: Let’s first create a Xcode project by selecting a template for our project Cocoa Touch Framework from iOS tab under Framework & Library section. Click next and give your framework name as “RandomRGB ” and save to your disk.

Image1

Step 2: Create a New swift file in your project to add some functionality in framework.This can be done by- Right click on project folder and select New File. Select Swift file from iOS tab and click next. Provide name of file as “RandomColor.swift” and save in our project directory.

Write the following code in “.swift” file.

framework2

Step 3: Now lets change the build configuration to release mode by navigating to Product->Scheme->Edit Scheme.If we want to use this framework in our application we need to switch to Release mode.The framework will be ready for release by running the project.

Step 4: Run the project and framework will be visible in products folder as shown in above image.Right click on it and select show in finder.Copy .framework file and paste it in desktop.Close the project.

Step 5: Create new project by selecting “Single View Application” template in Xcode project to use the framework.Give name to the project as “FrameworkTest” and click on next and save it.

Step 6: Add the framework that is present in desktop to “ ”project by creating folder in it as shown in below.

Frameworks1

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.

Image6_1


Step 7:
Import your framework by writing import RandomRGB in ViewController.swift.Open ViewController.swift file and in viewDidLoad() write following code.

Image7 (1)

The ouput of this application will be displaying the random colors to view background through the framework method call.

Run the application by cmd+R and view the output with Random RGB colours to the view.

Image7_1 Image7_2

We can distribute the framework by cocoa pods and you can publish your pod on GitHub and use a third party framework.

Google Certified Agency

about the author

sridivya

  1. Gowdesh Patil

    March 28, 2017

    Awesome… Good blog…