Search code examples
iosxcodeframeworksstatic-librariesdynamic-library

Library? Static? Dynamic? Or Framework? Project inside another project


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.


Solution

  • First, some general definitions (specific to iOS):

    Static library, formally Static Shared 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, formally Dynamic Shared Library - binds symbols at runtime. When symbol is resolved, the corresponding module that includes that symbol is linked.

    It's recommended to use Dynamic Shared libraries due to a lot of advantages. However, the app launch was slightly slower in earlier version of Mach-O than the app with static libraries. Latest versions are much quicker than the app with static libraries.

    Apple does not recommend to use Standalone Dynamic Shared libraries - this kind of Dynamic Shared libraries makes sense for macOS platform only (the library is stored at /use/lib and only Apple can formally do that), but non-standalone Dynamic Shared libraries are parts of any framework today (.framework bundle).

    Framework (aka .framework file) - is just a bundle (same as app bundle with minor differences). It's a folder with a group of resources:

    • dynamic/static shared library;
    • nib files;
    • localised strings;
    • header files;
    • documentation;
    • asset files, etc.

    Hence, you can actually have a static framework or a dynamic framework, which are just containers with either static shared library or dynamic shared library.

    See the Wiki on Software Framework for more details.

    Hence on iOS, your only option is basically to use a static shared library (.a file) or framework (with static or dynamic shared library inside).

    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/