Fix: Choose Between Static Library, Dynamic Library, or Framework in Xcode iOS Projects

Fix: Choose Between Static Library, Dynamic Library, or Framework in Xcode iOS Projects

Written by

in

Quick answer: For iOS, use a framework (.framework) instead of a raw static (.a) or dynamic library. Frameworks bundle code with assets, headers, and resources. Add the framework as a subproject, set it as a target dependency, and embed it in your app’s Frameworks, Libraries, and Embedded Content section.

Why this happens

Developers often start a feature as a separate Xcode project for isolated testing, then need to integrate it into a main app. The confusion stems from three similar-sounding options: static libraries (.a), dynamic libraries (.dylib), and frameworks (.framework). On iOS, the rules differ from macOS:

  • Static libraries link at compile time but cannot contain assets (images, nibs, localized strings). You would need a separate media bundle.
  • Standalone dynamic libraries are not recommended by Apple for iOS; they exist mainly for macOS system libraries in /usr/lib.
  • Frameworks are bundles that can hold either a static or dynamic library plus resources—headers, assets, documentation, and more. This makes them the natural container for shareable iOS code.

Because your sharing code likely uses assets or storyboards, a plain static library will not work without extra bundle setup. A framework solves this in one package.

How to fix it

1. Create a Framework target (recommended)

  1. In your sharing-code project, add a new target: File → New → Target → Framework (choose iOS).
  2. Name it (e.g., SharingServices).
  3. Move the relevant Swift/Objective-C files, asset catalogs, and storyboards into this target (Target Membership in the File Inspector).
  4. Build the framework target to verify it compiles.

2. Add the framework project as a subproject

  1. Open your main app project.
  2. Drag the sharing-code .xcodeproj file into the Project Navigator of the main project.
  3. In the main app target → General → Frameworks, Libraries, and Embedded Content, click + → Add Other… → Add Files… and select the framework product (SharingServices.framework) from the subproject’s Products folder.
  4. Set Embed & Sign (for dynamic frameworks) or Do Not Embed (for static frameworks). Most modern iOS frameworks are dynamic; choose Embed & Sign.

3. Configure target dependencies and build order

  1. Still in the main app target → Build Phases → Target Dependencies, click + and add the framework target (SharingServices).
  2. This ensures Xcode builds the framework before the app.
  3. If you see “No such module” errors, add $(BUILT_PRODUCTS_DIR) to Framework Search Paths in Build Settings (usually automatic).

4. Import and use

import SharingServices

// Use your sharing classes
let sharer = ImageSharer()
sharer.post(to: .instagram, image: uiImage)

5. Alternative: Static framework if you must avoid dynamic linking

If you need a static framework (rare, e.g., for App Extensions that cannot embed dynamic frameworks):

  1. In the framework target Build Settings, set Mach-O Type → Static Library.
  2. Keep Build Libraries for Distribution = Yes for Swift module stability.
  3. In the main app, choose Do Not Embed for that framework.

How to prevent it

  • Default to frameworks for any reusable iOS code that includes resources.
  • Use Swift Package Manager for net-new shared code; it creates framework products automatically and handles dependencies cleaner than subprojects.
  • Keep one source of truth: develop the framework in its own repo, then add it via SPM or as a subproject—avoid copying files.
  • Set BUILD_LIBRARY_FOR_DISTRIBUTION = YES on framework targets to generate stable .swiftmodule files across Xcode versions.

FAQ

Can I use a dynamic .dylib directly in an iOS app?

No. Apple rejects apps that link against standalone dynamic libraries not embedded in a framework bundle. Always wrap dynamic code in a .framework.

What if my framework contains only Swift code and no assets?

A framework is still preferred—it provides a proper module map, versioning, and resource bundle support if you add assets later. A static library offers no advantage here.

How do I share the framework across multiple apps without duplicating the subproject?

Publish the framework as a Swift Package (local or remote) and add it via File → Add Package Dependencies… in each app. This avoids subproject maintenance and works with CI.

Summary

Need Use Why
Code + assets, single app Dynamic framework (subproject) Bundles everything, embeds cleanly
Code + assets, multiple apps Swift Package → Framework Versioned, cacheable, no subproject mess
App Extension + main app Static framework or XCFramework Extensions cannot embed dynamic frameworks
Pure C/Obj-C, no resources Static library (.a) + media bundle Legacy support only

Start with a dynamic framework target, add it as a subproject (or better, a Swift Package), and embed it in your app. This keeps your sharing code testable in isolation and shippable in the main app.

Sources & credits

Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.