Should I create static libraries for iOS?

There is a code that I want to include in most of my projects. Things like AFNetworking, categories for CoreData and unit testing, etc.

It seems logical to include all this in a static library, and then use it in every project. I noticed that many third-party libraries (for example, AFNetworking and the predecessor ASIHTTP) are included in the projects, copying all the source files, and then manually linking the necessary libraries to the project goal.

It seems to me that this is the easiest way. It took a long time to figure out how to incorporate an existing static library into a project. Even after I knew, it still seems a pain for this for every new project. In addition, the header search paths you specify apply to the local directory with the static library files. Wouldn't it be easier, and is there a way to copy static library files into a project? This is the same idea as including class files directly, as most libraries do, but it would be more organized, because everything would be concentrated in one library project, instead of having class files everywhere and including all of them.

Static libraries believe that they should be the right way. Create a library that can be used with all projects that include the classes needed for each project. Has the meaning. I just argue because it seems right to leave everything from the β€œformal” library and just copy all the class files.

I think I'm just looking for what experienced developers consider the best option.

+7
source share
4 answers

I would be the first to admit that the process of referencing a static library in Xcode is not completely intuitive. However, using a static library is the best option, no doubt.

The main reason is maintainability: when you copy the source code of a library to many places, you must remember to update them to the latest code when upgrading to the next version of the library. This can be a fairly error-prone process, especially when the main source of the library changes significantly (for example, new files are added, old files are renamed, etc.).

+7
source

There is a solution halfway here - create an Xcode project that creates your static library from the source and puts it in a shared repository (i.e. .. git subodule, etc.), which is included in each main project repository.

Each of your projects will include this submodule and project. Then they get the latest source code each time they pull this submodule. If you set this as an assembly dependency, it will create a static library the first time it is created, and then Xcode is smart enough to include it in every subsequent assembly so you can quickly use the build time.

You also get the advantage that the source is where there is debugging / debugging capability.

If the source file is added or removed in a separate Xcode project and a new version of the library, you only need to change this common project - all your individual projects will not change at all.

+3
source

How about using CocoaPods ? This tool does exactly what you want in a declarative way: you have a file (subfile) where you declare your dependencies, and the tool loads all the dependencies and creates a static library that is added to your project.

+1
source

I would agree that static libraries believe that they can be the right way for a number of reasons, but they can also introduce some problems.

Positive would create an easy way to add a library to a project. Although this is not completely intuitive, it is quite simple to add a static library to the project after it has done this several times. Add files, add search path, execute. It may also be useful in some version control situations. In addition, updating the library may be easier.

I think the real issue here is for the open source community. Including, say, AFNetworking, for example, as a static library, you lose all access to the implementation files. This is a great opportunity to include a source, not a library. This allows you to change the code as you see fit, and hopefully return it.

0
source

All Articles