Understanding Static Library vs Dynamic Library in iOS Swift

Papon Smc
TakoDigital
Published in
5 min readJun 9, 2023

*** This section I practice use English language write blog

JARNBOY say “Hello everybody”, Welcome to share knowledge session for understand about Static Library and Dynamic Library for iOS developer

Introduction

When developing iOS applications using Swift, it’s crucial to understand the concept of libraries and how they can be utilized to modularize code and enhance the overall development process. In the iOS ecosystem, two common types of libraries are static libraries and dynamic libraries. In this blog post, we’ll delve into the differences between static libraries and dynamic libraries and explore their benefits and use cases in iOS Swift development.

Static Libraries:

Apple Document

A static library, as the name suggests, is a library that gets linked directly into an application at compile-time. It is essentially a collection of pre-compiled object files bundled together. When you compile your application, the necessary code from the static library is incorporated directly into the resulting executable. This means that the library’s code becomes a part of your application, resulting in a single, standalone executable file.

Benefits of Static Libraries:

  1. Simplified Distribution: With a static library, you can distribute a single executable file, simplifying the deployment process. Users only need to install and run the application without worrying about additional library dependencies.
  2. Performance: Static libraries can potentially offer better performance as the library code is directly integrated into the application, avoiding the overhead of runtime linking and loading.
  3. Code Protection: Since the library code is embedded within the application, it’s less prone to reverse engineering or tampering. This can be advantageous when dealing with proprietary or sensitive code.

Dynamic Libraries:

Apple Document

On the other hand, dynamic libraries are separate binary files that are linked to an application at runtime. These libraries are not included directly in the application binary but are loaded into memory when needed. iOS uses dynamic libraries in the form of Dynamic Link Libraries (dylibs) or Frameworks.

Benefits of Dynamic Libraries:

  1. Shared Resources: Dynamic libraries allow code sharing among multiple applications. If multiple applications on the same device use the same dynamic library, the library file is shared in memory, reducing the overall memory footprint.
  2. Versioning and Updates: Dynamic libraries can be updated independently of the applications that use them. This allows developers to fix bugs or introduce new features in a library without requiring updates to all the applications that depend on it.
  3. Dynamic Loading: Dynamic libraries are loaded into memory at runtime, which means they can be loaded or unloaded as needed. This dynamic loading capability is particularly useful for plugins or modules that can be added or removed from an application without modifying the core application code.

Choosing Between Static and Dynamic Libraries: The choice between static and dynamic libraries depends on several factors:

  • If you have control over the entire application, using static libraries can simplify distribution and provide better performance.
  • When developing reusable components or frameworks that need to be shared among multiple applications, dynamic libraries are a better choice.
  • If your application relies on external libraries or frameworks, you’ll need to consider their requirements and whether they are available as static or dynamic libraries.

How to specify a library as Static or Dynamic?

Specifying a library as static or dynamic is relatively simple. Select your framework target -> Build Setting -> Search “Macho” in the search field, then you can select from the drop down to make your library as static or dynamic.

Static or Dynamic library in action

Here we are going to see a practical example for how a static library and dynamic library affects the executable size of the host app. On clicking show package contents of the archived file of the host app, we can observe the below

  1. Static library: Keeping the Macho of the framework as ‘Static Library’ and embedding in to host app, the library code is copied into the host app’s executable and the host app executable size is 680 KB.

Host app executable + statically linked library code = 680 KB

Host app executable with Static linking

2. Dynamic Library: Keeping the Macho of the framework as ‘Dynamic Library’ and embedding in to host app, the host app doesn’t copy the library code into its executable and the app’s executable size is independent of the library.

The host app executable size is now 432 KB.

Host app executable with Dynamic linking

Note: Depending on your need whether you need to access the resources of your library such as storyboards, image files, and localized strings, you can either embed or do not embed the static library to your host app. In the case of dynamic linking you need to embed as the library needs to be in your bundle for the dynamic loader to identify the modules. Embedding affects your .ipa size but does not affect executable binary size. Executable binary size is affected only by static or dynamic linking.

Conclusion

Understanding the differences between static libraries and dynamic libraries is essential for iOS Swift developers. Static libraries provide simplicity, performance, and code protection, while dynamic libraries offer code sharing, versioning flexibility, and dynamic loading capabilities. By choosing the appropriate type of library based on your project’s requirements, you can optimize your development process and create efficient, scalable iOS applications.

You can feedback me for develop myself

Best Regrads,
Papon Supamongkonchai
iOS Developer

--

--

Papon Smc
TakoDigital

IOS Developer like learn for develop skill my self road to specialist Mobile developer and can give consult every bussiness , tech and architect question