Building iOS Framework With Dependencies. - by Max Kalik - ITNEXT
Building iOS Framework With Dependencies. - by Max Kalik - ITNEXT
Open in app
Get unlimited access to the best of Medium for less than $1/week.
Become a member
https://itnext.io/building-ios-framework-with-dependencies-e6e141f346ec 1/22
8/28/23, 8:02 PM Building iOS framework with dependencies. | by Max Kalik | ITNEXT
Final product
The task is to create a simple framework only for real devices with
dependencies that can be installed via cocoapod. Where we could
https://itnext.io/building-ios-framework-with-dependencies-e6e141f346ec 2/22
8/28/23, 8:02 PM Building iOS framework with dependencies. | by Max Kalik | ITNEXT
use this framework? In Unity! You can read my article about this
here. But let’s move forward and start to create our final product.
Podfile
Assume, we created a new iOS Framework project. I named it
MyFramework and in the terminal run pod init in the root of the
project folder, so we have Podfile where we can start. Let’s configure
it:
source 'https://cdn.cocoapods.org/'
source 'https://github.com/passbase/cocoapods-specs.git'
source 'https://github.com/passbase/microblink-cocoapods-specs.g
target 'YourFramework' do
pod 'CropViewController', '2.6.1'
pod 'Kingfisher', '7.5.0'
pod 'Intercom', '14.0.6'
pod 'Frames', '4.0.4'
pod 'Passbase', '2.7.0'
end
I didn’t use some abstract cocoapods, I took the real ones on purpose
to show you the real process which you could repeat by yourself. At
the first sight of the list of dependencies, you could say that they are
picked randomly. And yes and no, because all these dependencies
are different ones, for example:
https://itnext.io/building-ios-framework-with-dependencies-e6e141f346ec 3/22
8/28/23, 8:02 PM Building iOS framework with dependencies. | by Max Kalik | ITNEXT
itself
Resources
If a framework uses resources (images, video, or other files), then we
need to prepare a separate target for it and this process looks a little
bit like a workaround. Let me describe it step-by-step:
1. Open File / New / Target. Choose macOS and in the Framework &
Library section choose Bundle.
https://itnext.io/building-ios-framework-with-dependencies-e6e141f346ec 4/22
8/28/23, 8:02 PM Building iOS framework with dependencies. | by Max Kalik | ITNEXT
2. For now, make sure the resource files belong to this target.
https://itnext.io/building-ios-framework-with-dependencies-e6e141f346ec 5/22
8/28/23, 8:02 PM Building iOS framework with dependencies. | by Max Kalik | ITNEXT
Build
https://itnext.io/building-ios-framework-with-dependencies-e6e141f346ec 6/22
8/28/23, 8:02 PM Building iOS framework with dependencies. | by Max Kalik | ITNEXT
xcodebuild archive \
-workspace MyFramework.xcworkspace \
-scheme MyFramework \
-configuration Release \
-sdk iphoneos \
-archivePath archives/ios_devices.xcarchive \
BUILD_LIBRARY_FOR_DISTRIBUTION=YES \
SKIP_INSTALL=NO \
This simple script creates an archive. Open it with the right mouse
button using “Show content” and find a fresh-backed framework in
the products.
Now the framework is ready to be tested. You can just drag this
framework to your test iOS project structure. Don’t forget to Embed
and Sign this framework. Choose a real device where we are going to
test our app with this framework.
https://itnext.io/building-ios-framework-with-dependencies-e6e141f346ec 7/22
8/28/23, 8:02 PM Building iOS framework with dependencies. | by Max Kalik | ITNEXT
The result will be not so good. The project will give you an error —
Build failed.
The reason is our dependencies are not found, they weren’t included
in our framework. So let’s solve this problem.
source 'https://cdn.cocoapods.org/'
source 'https://github.com/passbase/cocoapods-specs.git'
source 'https://github.com/passbase/microblink-cocoapods-specs.g
target 'YourFramework' do
use_frameworks! :linkage => :static
end
The next step is not very obvious (I would say even questionable)
because we need to include all dependencies’ frameworks to our
framework literally by copying them from the Products folder of
Pods into our project.
If doing this manually, you need to Run the framework project first,
then you will get the Products folder with frameworks from each pod
including their bundles.
https://itnext.io/building-ios-framework-with-dependencies-e6e141f346ec 9/22
8/28/23, 8:02 PM Building iOS framework with dependencies. | by Max Kalik | ITNEXT
From each dependency folder, you need to find a folder with a name
something like Name.framework, and drag and drop it to your
Framework project. By the way, we don’t need to include frameworks’
resource bundles in our project because they will be included
automatically.
https://itnext.io/building-ios-framework-with-dependencies-e6e141f346ec 10/22
8/28/23, 8:02 PM Building iOS framework with dependencies. | by Max Kalik | ITNEXT
But, even here we can come across with small nuance. As you
remember some of our dependencies are XCFramework, and the
folder where supposed to be these frameworks are empty.
Among all the product folders you can find a specific one called:
XCFrameworkIntermediates where you can find all frameworks from
XCFrameworks.
https://itnext.io/building-ios-framework-with-dependencies-e6e141f346ec 11/22
8/28/23, 8:02 PM Building iOS framework with dependencies. | by Max Kalik | ITNEXT
After all these manual manipulations, you can build your framework
again and get the final product which you can test. If you did
everything correctly, then your project with the new framework will
run successfully and all methods from the framework will work
without failures.
Potential problems
Traditionally, instead of a conclusion, which nobody cares about, I
think it will be better to list some potential problems and how to
solve them. Let’s get started.
Solution:
One of the most often errors. We are all people and we can just forget
to sign the framework.
https://itnext.io/building-ios-framework-with-dependencies-e6e141f346ec 12/22
8/28/23, 8:02 PM Building iOS framework with dependencies. | by Max Kalik | ITNEXT
Problem 3: Build failed. A client uses the same dependencies which your
framework already uses.
Solution:
Briefly: Versions of the dependencies in a framework and in an app
should be the same.
Detailed: Let’s say you build your framework with cocoapod
Alamofire 5.5.0 and a client app will use your framework and
Alamofire 6.0.0 , so this combination will cause a problem. In other
words, you cannot build your app, and you won’t understand what is
going on, because the error will be about symbols, or something else.
Problem 4: The path to your product is not found (the product folder in the
archive is empty)
Solution:
Check flag: SKIP_INSTALL=NO in your build script — it will install your
framework in the archive, in other words, your product folder won’t
be empty.
https://itnext.io/building-ios-framework-with-dependencies-e6e141f346ec 13/22
8/28/23, 8:02 PM Building iOS framework with dependencies. | by Max Kalik | ITNEXT
Solution:
The best way to avoid this is if the names of modules in a framework
are different and don’t coincide with the name of the framework
itself. So, if you just started your framework from scratch — keep
your public module name different. For example, our framework is
called MyFramework.xcframework so the name of your general module
could be MyFrameworkMethods
Follow
iOS Developer
https://itnext.io/building-ios-framework-with-dependencies-e6e141f346ec 14/22
8/28/23, 8:02 PM Building iOS framework with dependencies. | by Max Kalik | ITNEXT
326 1
2.6K 31
232 2
https://itnext.io/building-ios-framework-with-dependencies-e6e141f346ec 16/22
8/28/23, 8:02 PM Building iOS framework with dependencies. | by Max Kalik | ITNEXT
393
157
Mobile@Exxeta
https://itnext.io/building-ios-framework-with-dependencies-e6e141f346ec 18/22
8/28/23, 8:02 PM Building iOS framework with dependencies. | by Max Kalik | ITNEXT
Lists
Icon Design
30 stories · 64 saves
https://itnext.io/building-ios-framework-with-dependencies-e6e141f346ec 19/22
8/28/23, 8:02 PM Building iOS framework with dependencies. | by Max Kalik | ITNEXT
Lakshminaidu C
https://itnext.io/building-ios-framework-with-dependencies-e6e141f346ec 20/22
8/28/23, 8:02 PM Building iOS framework with dependencies. | by Max Kalik | ITNEXT
Di Nerd 🧑🏿💻
Swift Package Manager vs. CocoaPods: The Battle of the
Dependency Managers ⚔️
A Comprehensive Comparison of the Pros and Cons of Each for iOS
Developers
https://itnext.io/building-ios-framework-with-dependencies-e6e141f346ec 21/22
8/28/23, 8:02 PM Building iOS framework with dependencies. | by Max Kalik | ITNEXT
https://itnext.io/building-ios-framework-with-dependencies-e6e141f346ec 22/22