Framework Creation And Code Visibility Control Using Binary

Kaneriya bhargavkumar
Simform Engineering
6 min readMar 28, 2024

Framework creation in Swift can be a powerful and secure way to modularize your code.

In Swift, a framework is a modular and reusable set of code and resources that can be incorporated into various projects. What if I tell you you can build your own framework by following a quick and easy setup?

Frameworks allow developers to encapsulate functionality, promote code reuse, and maintain a clear separation of concerns. They are commonly used for sharing code across multiple projects within a single organization and a larger community of developers. They can also help manage code security using binary creation.

Table of Contents

  • Essential Concepts: Prerequisite Terms for Understanding Our Blog
    ⋆ Framework Creation
    ⋆ Code Visibility Control
    ⋆ Compiled Binary
  • Why do we need to do code visibility control
  • How do we can create a framework using binary creation
  • Testing
  • Conclusion

Essential concepts: Prerequisite terms for understanding our article

Framework Creation

The process of creating a framework involves grouping concepts, practices, and libraries of reusable code to streamline software development and encourage code reuse and maintainability.

Frameworks simplify development by encapsulating shared features.

Code Visibility Control

Encapsulation, modularity, and confidentiality in programming all depend on controlling code visibility. It defines which code segments are available, hides implementation specifics, guards against accidental use, and encourages clear interfaces.

Compiled Binary

These binaries are ideal for distribution and execution on target systems because they contain instructions in a format that the computer’s processor can directly execute, removing the need for the original source code. Once a program is converted into a binary format that a computer can directly execute, it becomes challenging for someone to reverse-engineer the original source code.

Why do we need to do code visibility control?

Controlling code accessibility in frameworks through binary optimization is essential for many reasons, including:

  • Protection of intellectual property: Compiled binaries prevent theft and reverse engineering by securing proprietary code.
  • Security: It limits code access, minimizing security holes and unwanted access.
  • Encapsulation and modularity: These facilitate improved organization by maintaining tidy interfaces and concealing implementation difficulties.
  • API stability: It provides stable interfaces by enabling framework upgrades without impacting user code.
  • Performance: The overall performance of the framework is enhanced through better execution.

How do we create a framework using binary creation (to control the code visibility)?

Create a new Xcode project by selecting the Framework template.

Create A New Xcode Framework Project

For this blog, I have created a framework project named "SimformSDK.”

Framework Based Project

Here is some quick information to help you understand the process better:

  1. Created “Simform.swift” as the Base or Root class for accessing framework functionality.
  2. Implemented a protocol-oriented approach for accessibility restrictions.
  3. Developed “SimformBaseProtocol.swift” to define protocols for interacting with the framework.
  4. The protocols specify the required functionalities and access levels for framework usage.
  5. The “Simform.swift” class acts as the entry point for developers to access and utilize framework features, adhering to the defined protocols for restricted access.

Note: Make sure you have not given the name of the class exact same as your Framework name (in our case, it can not be SimformSDK). It may give you error of name conflict while you release your framework binary.

Framework Base Class

Only functions or variables provided in the SimformBaseProtocol can be accessed by the end user who will use your framework.

Framework Base Protocol

Now, you are done with the basic framework setup.

Let’s explore binary creation

This method offers many benefits, including ease of integration, increased security, and improved performance. In this guide, we’ll walk you through the process of creating a Swift framework and packaging it as a binary for distribution.

For this blog, we will make a script that will create a binary framework for you. For that, you need to configure a file once, and then, with a single click, you can create the Binary of the framework.

Note: Make sure you place this script in the root where your project is located. Also, the below scripts are in a single file and I divided that script in 3 parts for the better understanding.

SCRIPT Part 1

Pick the right script below for .xcworkspace or .xcodeproj, whatever you use. For our SimformSDK demo, we are using an .xcodeproj.

Located .xcodeproj File

If you are going with .xcworkspace, use the below content:

SCRIPT Part 2

If you are going with a .xcodeproj, use the below content:

SCRIPT Part 2

Finally, add the below script to remove the created .xcarchive(s).

SCRIPT Part 3

You need to update the following aspects based on your project structure and setup:

PROJECT_NAME : Name of your .xcproject or .xcworkspace

TARGET_NAME: Your project target that you want to create a build for

XCframeworkFolder: It can be anything — in this folder, we have our compile version of the framework.

That’s it. Now, you just double-click on the “Binary_Creation.command” file. It will take some time to complete the script. Once you get the message shown below, you can close the terminal.

Note: You may have the permission issue while executing the Binary_Creation.command file. So make sure you have given the proper access for .command file.

You can execute below command in terminal :
chmod +x Path_To_Your_CommandFile

Binary_Creation.commad file Success Result

Now, go to your project folder and look for the XCframeworkFolder folder you created. In my case, it’s Simform_XCframework.

XCFramework Folder Structure

🎉 Celebrate the arrival of SimformSDK.xcframework — your golden ticket to effortless integration and unparalleled coding adventures!

Let’s do the testing now

Create a new, fresh Swift project to test the created framework, and drag and drop the created xcframework. Ensure that the “Copy items if needed” option is checked.

How To Test Framework

Add the below code to the initial setup for how to use the created framework:

Example App ViewController File

Finally, when you run the project, you will get the below output:

Framework Test Project Result

Testing results:

  1. Initialization Status is initially set to false.
  2. Upon calling Simform.shared.initialize() in the framework's "Simform.swift" file, the status is updated to true.
  3. For demonstration purposes, we added a manual delay of 2 seconds.
  4. After the delay, the Initialization Status will reflect as true.

In general, when building the framework, the initialize function is used to check the basic validation of the framework. Like private-key validation, third-party dependency checks, analyzing prerequisites for the framework, etc.

GIF by Tenor

Conclusion

In conclusion, combining framework creation, careful management of code visibility, and distributing binaries marks a big step forward for developers. By organizing code into frameworks and controlling who can access it, developers make it easier to reuse and protect sensitive parts of their code. Sharing these frameworks as binaries speeds up collaboration and helps create a strong community of developers. These practices improve how code is maintained and scaled, giving developers more confidence in their work. Overall, these advancements in software development open the door for more innovation and excellence.

Bonus : If you want to learn how to release a compiled version of the SDK, there is a very good blog written by Shubham Vyas about that. Here is the guide you can refer to:

https://medium.com/simform-engineering/unified-swift-swiftui-library-deployment-2f39dc76b7f5

Follow Simform Engineering to keep yourself updated with the latest trends in the technology horizon.

Follow us: Twitter | LinkedIn

--

--