180

I have an existing iOS app and want to add a large chunk of code that I've been developing as another project just for ease of testing. The new chunk basically deals with saving an image to various sharing services, etc.. Because that sharing code needs a lot of testing and future updating, I was wondering what the best way to incorporate that code chunk into my existing app.

I don't know if it should be a static library, dynamic library or a framework, and honestly, I'm not really sure what the difference is, or how I should go about it and get it set up in Xcode.

All I know is that I need/want to keep a separate testing and updating app for the sharing code and have the main app use it.

Honey
  • 24,125
  • 14
  • 123
  • 212
pizzafilms
  • 3,195
  • 3
  • 18
  • 33

3 Answers3

227

First, some general definitions (specific to iOS):

Static library - a unit of code linked at compile time, which does not change.

However, iOS static libraries are not allowed to contain images/assets (only code). You can get around this challenge by using a media bundle though.

A better, more formal definition can be found on Wikipedia here.

Dynamic library - a unit of code and/or assets linked at runtime that may change.

However, only Apple is allowed to create dynamic libraries for iOS . You're not allowed to create these, as this will get your app rejected. (See this other SO post for confirmation and reasoning on such).

Software Framework - a compiled set of code that accomplishes a task... hence, you can actually have a static framework or a dynamic framework, which are typically just the compiled versions of the above.

See the Wiki on Software Framework for more details.

Hence on iOS, your only option is basically to use a static library or static framework (the main difference being that a static framework is distributed as a compiled .a file most often, whereas a static library may simply be included as a subproject - you can see all of the code - which is compiled first and its resulting .a file used as a dependency by the project).

Now that we're clear(er) on these terms, setting up a static library and supporting media bundle for iOS isn't too difficult, and there are many tutorials on how to do such. I personally would recommend this one:

https://github.com/jverkoey/iOS-Framework

This is a pretty straight-forward guide and doesn't have the disadvantage of dealing with "fake static libraries"... check it out for more info...

Once you've created your static library, it's as easy as including it as a submodule within Git for use across different projects.

Good Luck.

EDIT

Regarding a subproject within a project, as far as I know, to get this to work/compile correctly, you essentially have to set up a compile chain where the subproject is compiled first, which creates a static framework .a file that is used as a dependency by the project.

Here's another useful tutorial which talks about this:

http://www.cocoanetics.com/2011/12/sub-projects-in-xcode/

EDIT 2

As of iOS 8, Apple now permits developers to create dynamic frameworks! (Note: your app must have a minimum target of iOS 8 to include a dynamic framework... back porting isn't allowed.)

This has been added as a new project template. In Xcode 6.1, this can be found at:

New Project -> iOS -> Framework & Library -> Cocoa Touch Framework
Community
  • 1
  • 1
JRG-Developer
  • 11,675
  • 8
  • 54
  • 79
  • So far, it seems that the subproject is what I want and that article was perfect. I've noticed one odd side effect: The subproject that I dragged inside my main project also has my testing code (viewcontroller and nib, appdelegate, etc), and I've made sure that just the classes that I want to use in the main project are checked to be used in the static library. But for some reason, when I went to make attachments to my main project's nib file, it also showed outlets and actions from my subproject. This could definitely lead to some confusion. Any tips to get rid of those? Thanks! – pizzafilms Mar 11 '13 at 16:26
  • 1
    Can a dynamic project be dragged and dropped into a static project, thereby making it a static project? I'm really confused, some clarification would be really great! Thanks in advance :-) – Ravindranath Akila Aug 26 '13 at 01:54
  • 1
    @JRG-Developer Back porting dynamic framework is allowed if you follow some rules : https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html#//apple_ref/doc/uid/TP40014214-CH21-SW3 – klefevre Apr 17 '15 at 13:41
  • Is it possible to set a lower minimum target and make the library optional? – kukudas May 04 '15 at 15:04
  • 1. can you include some well-known examples of static library, dynamic library, framework? 2. Can you give examples of where you would need to do such? 3. Curious what's the difference between a pod and a static library? – Honey Jul 13 '18 at 17:43
  • Does this still hold true for Swift libraries? Is it different? – CyberMew Dec 16 '20 at 11:44
56

Mach-O file format(Mach Object - .o)

In iOS world every source file is converted into object files - ABI[About] Mach-O file[About] which will be packaged into a final executable bundle(application, framework), file (library) and it's behavior is determined by Mach-O type[About]

Package is a directory which behavious itself as a file - opaque file. It is created for user experience to complicate making some changes into internal structure that can cause unpredictable program behaviour. Package is used in Document Package or with a Bundle. You can use Show Package Contents in a Finder

Bundle is a directory with a specific structure to organize a binary(executable code) and resources for that code(e.g. images, nibs...). Bundle contains Info.plist[About] file. Bundle was created for developer experience. Also it can be packaged. There are several types of bundle:

  • application bundle - Application target
  • framework bundle and versioned bundle as a subtype - Framework Target
  • loadable bundle(aka plug-in bundle) - '... Bundle' (UI Testing Bundle, Unit Testing Bundle) - can be loaded at runtime. .bundle extension for Mac OS
  • [Mac OS] XPC Service - Cross Process Communication is a kind of Inter Process Communication (IPC). It can be used as a module on a different process(managed by launchd root process)[About]
  • others(dSYM[About] bundle)

Application - .ipa, .app[About] - packaged application bundle - launchable program.

Application extension[About] - from iOS v8 - extends functionality of Application which are available when user interacts with other application. App extension as a bundle is a part of Containing app but it is run on their own sandbox(processor, memory...), app which try to use app extension is called Host App. Types of extension app:

  • Action
  • Share
  • Photo Editing
  • Today aka widget
  • ...

to share common code and resources. It's available when Deployment target is iOS 8+.

Tests - packaged loadable bundle which is used to test a binary. Plug-in architecture allows us to add a new functionality(test cases) as a separate module into existing binary

Libraries and Frameworks

[Library vs Framework]

Martin Fowler on InversionOfControl

A Library is essentially a set of functions that you can call, these days usually organized into classes. Each call does some work and returns control to the client.

A Framework embodies some abstract design, with more behavior built in. In order to use it you need to insert your behavior into various places in the framework either by subclassing or by plugging in your own classes. The framework's code then calls your code at these points. The main control of the program is inverted, moved away from you to the framework. This phenomenon is Inversion of Control (also known as the Hollywood Principle - "Don't call us, we'll call you"

Libraries and Frameworks on iOS

They can help you to solve: modularity, reusing, encapsulation, improve build time

Library is a collection of Mach-O object files[check static or dynamic] compiled for one or more architectures.

Static library - .a(aka static archive library, static linked shared library[doc]) - When you add it into your application the static linker during compilation time will merge the object files from the library and package them along with the application object files into one single executable file. The disadvantage is a big output file

From Xcode 9.0, Swift static library is supported.

Dynamic library - .dylib(aka dynamic shared library, shared object, dynamically linked library[doc]) is dynamically linked with the app's executable at load or runtime, but not copied into it. On practice app's package will contain Frameworks folder with .dylib file. All iOS and macOS system libraries are dynamic. The disadvantage is a slow launch time since all dynamic libraries should be copied and linked.

[Static vs dynamic linking]

Text-based stub library - .tbd[About], it is a text stub of dynamic library which is located on a target device. As a result you should not package a dynamic library into your bundle. It has a size effect.

Framework aka binary framework - .framework is a not packaged framework bundle(to allow developers to easily take a look at headers and resources) which contains a compiled static or dynamic library, header files and resources.

Static framework contain a static library packaged with its resources.

Dynamic framework aka Embedded framework - from iOS v8 - contains the dynamic library and resources. In addition to that, dynamic framework can include different versions of the same dynamic library in a single bundle (versioned bundle). Also Embedded framework is used in App Extension

[Static vs dynamic framework]

Umbrella framework [Aggregate target] is a framework that contains other frameworks. It is not officially supported on iOS and that is why it is not recommended for developers to create them[Official doc]. In actuality it's a set of sub-frameworks(or Nested Frameworks). When you create a framework which has a dependency, a consumer (such as an app) is responsible for adding this dependency along with your framework into the project. As a developer, it's natural to try to find a way to transfer this duty from consumer to your's. As a result you think that Umbrella framework is the rescue but usually it leads to a serious issues with managing versions and complexity of creating and supporting it.

Fake Framework - is a result of specific operations under a static library to create a bundle with .framework extension that will behave yourself as a dynamic framework. This technic was used when Xcode did not support creating a framework since did not have a framework template. One of realisation of a fake framework. With Xcode 6, Apple has added iOS framework support.

Modular Framework[About] - @import it is a framework which contains a .modulemap file inside. Module can contains submodules. The main advantage is that you save a build time with Modular Framework.

Universal Library or Framework (aka Fat) [lipo] [Aggregate target] contains multiple architectures. For example your release build should support a some arch which you can regulate via Build Active Architecture Only [ONLY_ACTIVE_ARCH]

XCFramework[About] was introduced by Xcode 11 and it is a bundle which includes multiple architectures(arm, x86_64...) and platforms(iOS, MacOS...). It should replace a Universal Framework

Dependency[About] You are able to use third party code as a part of your target. It allows you to reuse a code from a lot of sources like - another project, project in the same workspace, another target, library, framework etc.

How to build and use a Static Library:

How to build and use a Dynamic Framework[change to static]

[Xcode Build System]
[Xcode components]
[Dynamic linker]

yoAlex5
  • 13,571
  • 5
  • 105
  • 98
  • 1
    In many Swift tutorials its mentioned that Objective C doesn't support dynamic libraries, where as swift supports, [https://www.altexsoft.com/blog/engineering/swift-vs-objective-c-out-with-the-old-in-with-the-new/ ] But as I know i,OS8 onwards Objctive C supports Dynamic library. Can you plz clarify this ? – pratima Jan 14 '20 at 13:38
  • @pratima, you are able to create a dynamic framework on Objective-C for iOS – yoAlex5 Apr 07 '20 at 19:21
  • 1
    "Static framework contain a static library packaged with its resources." Where did this definition come from? AFAK, static framework's "Copy Bundle Resources" build phase does not work, just like static library. What's the difference between static framework and static library? – toshi0383 Apr 18 '20 at 13:07
  • @toshi0383 Did you find a way to merge static library(.a) and it's resources(.bundle) into a single framework that we can distribute to the consumer? – user121095 Jun 03 '20 at 11:06
  • @toshi0383, I explained my investigation results. The difference you can find here https://stackoverflow.com/a/57741985/4770877 – yoAlex5 Sep 07 '20 at 10:27
  • @user121095, this answer is not about it – yoAlex5 Dec 16 '20 at 10:20
2

You can also create .podspec file for CocoaPods( http://guides.cocoapods.org/making/private-cocoapods.html#1.-create-a-private-spec-repo ) and use it like any other pod with the only difference that it's your private pod and is not visible to outside world(I'm not sure what will happen if your pod should create CoreData model, but that's not the case, as I understand).

Timur Kuchkarov
  • 1,157
  • 7
  • 21