Ever wondered what is a framework? A framework is a basic structure that allows you to efficiently package up and re-distribute the code to different apps and developer teams. Well, in Swift, it is a hierarchical directory that encapsulates shared resources like nib files, image files, dynamic shared library, localized strings, and reference documentation in a single package.

Why do we actually need a framework? Frameworks eradicate the need to write repetitive code, saving a lot of time, and is a standardized implementation amongst developers. It is all about the maintainability and development speed of the applications. Another advantage is that scaffolding an application becomes easier and saves a great deal of time rather than manually adding all the components to it. Also, the framework gives a favorable environment for teamwork, because every person of the team follows common principles of development when using it. Thus making the development faster and more organized.

Some of the top-notch and widely used frameworks in Swift are :

CloudKit

It is Apple’s Backend as a service platform specifically designed for iOS apps including tvOS, macOS, and watchOS. CloudKit provides knife-edge and rich application programming interfaces which help to store and query your custom data in the cloud for your applications. It can be also used for web and server apps. CloudKit is the best option for iOS-only apps that use a lot of data and do not require a great deal of server-side logic.

AVFoundation

Apple released its first version of AVFoundation with the dawn of iOS 4, a multimedia framework with APIs supporting both Objective-C and Swift. It supports high-level services for time-based audio-visual media (editing and playing audio-visual media) in iOS, macOS, tvOS, and watchOS. AVFoundation is a complex and intrinsic framework, so before using it you really need to make sure whether you want to use this framework or not. In most cases, the usage of using Apple’s default APIs such as UIImagePickerController will suffice.

HomeKit

A software framework mainly advised for smart-home applications allowing users to configure, communicate with, and control smart-home appliances which are available in iOS & iPadOS. Applications built using HomeKit are already available in the market and the advancement and demand for the same are increasing day-by-day.

Creating a Swift Framework

While building an iOS app, breaking the project into different modules and frameworks can be a good strategy as a developer. Thus allowing developers to share code between multiple apps and amongst themselves.

Let's get started on the tutorial on how one can create a framework. We will be creating two very simple projects named: AnalyticFramework and MainApp. Now import the AnalyticFramework in the MainApp which will initialize the Analytics class, and then call the log method. It is very simple when it comes to creating and using a framework.

Step 1: Create a new framework

Open Xcode and create a new project. Choose Framework from Framework & library as shown in the image below.

Step 2: Name the project AnalyticFramework

The name for the project is AnalyticFramework:

Step 3: Add a class named Analytics to the Framework

Let's add a new file to the project which will have our Analytics class, to be used later on to log a message.

Make sure that it is a Swift file and name it as Analytics:

Step 4: Let’s write the code for the Analytics class

Add the following code to the newly created file.

public class Analytics {
public init() {}
public func log(message: String) {
print("Log message: ", message)
}
}

This is a simple class, since we want to use this class outside of our framework we need to mark it as public. We have a blank init method because we need the init to be public in order to initialize Analytics in the MainApp. As our application doesn't take any arguments, we will keep it blank and just make it public.
Add a log method to the Analytics class. Make this method public so that it can be used from another framework/project. The log method takes one argument, a message, which will print to the console.

Step 5: Create the MainApp

We can now create a single view application for our main app and name it MainApp:

Now drag and drop the AnalyticFramework.xcodeproj to the MainApp:

Make sure that you don't have any other instances of Xcode open while dragging AnalyticFramework.xcodeproj. Now you can see a little arrow next to it which points to the contents of that framework. As shown in this picture:

Step 6: Add the Analytics framework to Frameworks, Libraries, and Embedded Content

Click on the MainApp project -> General tab and then look for Framework, Libraries and Embedded Content. And then click on the + button. Refer to the following image to see what needs to be done:

You will get prompted to choose the framework you want to add when you click on the + button, which will look as follows:

Make sure to select the AnalyticFramework.framework as shown in the above image.

Step 7: Use the framework

Now that we are ready to use the framework. We can use it in the viewDidLoad in the ViewController file in MainApp.
Import the AnalyticFramework below the import UIKit as shown below:
import UIKit
import AnalyticFramework


Next update the viewDidLoad. Replace the current viewDidLoad with the following:
override func viewDidLoad() {
super.viewDidLoad()
let analytics = Analytics()
analytics.log(message: "analytics initialized")
// Do any extra setup after loading the view.
}


Save the file to build and run the app. When you do, it will print out the Log message: analytics initialized. And there we go, that is all needed to create a framework using Swift.

Author | Varsha Vinod | Application Developer

Leave a Reply

Your email address will not be published.

Comment

Name

Email

Url


Call Now Button