Best Way to distribute custom framework

So You have a solution to a problem that many other iOS developers have and you want to help them, but you also want to protect your code and build a business? Binary Framework. That’s what you want! You might have noticed that you can’t see the source code of the UIKit Framework you use to build your iOS apps on top of every day. That’s because Apple ships the UIKit as a binary framework in its iOS SDK.

Below are the available ways of creating Libraries or frameworks 

Umbrella Framework

In the Apple development environment, the incorporation of one framework into another is called “Umbrella”. This type of framework is meant to hide the use of some sub-frameworks and thus facilitate their use. 

Umbrella frameworks are available for macOS apps, but Apple does not recommend using them. Apple does not officially support the Umbrella Frameworks for iOS,watchOS, or tvOS, indeed citing the official documentation:

Don’t create Umbrella Frameworks.

While creating umbrella frameworks using Xcode is possible, doing so is unnecessary for most developers and is not recommended. Apple uses umbrella frameworks to mask some of the interdependencies between libraries in the operating system. In nearly all cases, you should be able to include your code in a single, standard framework bundle. Alternatively, if your code was sufficiently modular, you could create multiple frameworks, but in that case, the dependencies between modules would be minimal or nonexistent and should not warrant the creation of an umbrella for them.

Please refer “Apple Documentation”

Universal Framework(fat framework)

multi-architecture binary that contains code native to multiple instruction sets and can run on multiple architectures. In short, it contains code compiled for all the platforms which you’re going to support. For example x86_64 (64-bit simulator), arm64, armv7, armv7s for devices. As a result, such a framework will have a larger size than a one-architecture framework. There are lots of tutorials on how to make a universal framework using lipo. This approach is widely used for sharing a private binary. In this way, your framework consumer can work with your framework on a real device and simulator.

The loading of system frameworks is highly optimized. But loading custom embedded frameworks can be expensive. Apple’s engineers encourage you to use frameworks wisely and limit the number of used frameworks because it impacts the app launch time. If you are interested in deep details on how the framework works and how it impacts the app launch time, check the WWDC session Optimizing App Startup Time

This year Apple declare that Swift 5 provides binary compatibility for apps, which means that an app built with one version of the Swift compiler will be able to talk to a library built with another version.

XCFrameworks

XCframeworks is a new supported way to distribute binary frameworks, available from Xcode 11. A framework that can continue binary for multiple architectures and platforms. Just generate archives for different platforms and bundle them up together in a single XCFramework.If you want to deep dive there are a great session Binary Frameworks in Swift from WWDC 2019. 

There are a few advantages of using XCFrameworks:

  • XCFramework contains variants not only for device and simulator but for any of the platforms that Xcode supports: iOS, macOS, tvOS, watchOS.
  • It Supports Swift and C-based code.
  • Can bundle up frameworks and static libraries.

Swift Package

Swift is a cross-platform language and requires a cross-platform tool for building the Swift code. One of the goals of Swift Package Manager(SwiftPM) is to simplify distribution source code in the Swift ecosystem. SwiftPM is an open-source project, you can find information about it on Github as well.

Swift Package contains source files and manifest files (Package. swift). Manifest describes the configuration for the Swift package. Because of distribution in source form, there is no need to maintain binary compatibility for clients. So if you can ship the source code, then Swift Package is a great tool for you.

With the new package manifest API in XCode 12, it is now possible to make Swift Packages that include one or more XCFrameworks. The details of implementation you can find in the official documentation. Distributing Binary Frameworks as Swift Packages and WWDC 2020 Videos.

Swift Package consists of 3 parts: dependencies, targets, and products.

Dependencies: other Swift Packages, that you are using inside your package. In the Package file, each dependency is specified by source location and version.

Target: As Apple’s document says, Targets are the basic building blocks of a Package. It may be either a library or an executable as its product. Before XCode 12 Swift Package could contain only Swift, Objective C/C++, or C/C++ files. XCode 12  offers the new tool-version:5.3 which brings the new package manifest API. And now the Swift Package can contain other resource types: images, storyboards, JSON files, Shell scripts, and much more. You can also localize those resources. that is a great and desired improvement. Thank you to all who have contributed to making this available. Useful details and implementation details, you can find the WWDC 2020 videos, Swift Packages: Resources and localization, and in documentation Localizing Package Resources.

Products: The output of your package, either a library or an executable produced by a package, and make them visible to other packages.

Summary

You can see that nothing magical happens here, I described a way of creating Libraries to distribute your solution to the developer community, you just need to select the best option to distribute your solution, I hope it will help you.

Thank you very much for your precious time.

Hope you guys enjoy it.

Let me know your thoughts in the comment section.

Extend your knowledge

The Twitter contact:

Any questions? You can get in touch with me here

Leave a Reply

Your email address will not be published. Required fields are marked *