agarcian
agarcian

Reputation: 3965

Avoiding header conflicts when creating and depoying Static Libraries in iOS

I have created a static library in iOS where I have certain common functionality for my projects. I used the instructions for the iOS Universal Framework and everything works great.

I have a module in the static library that requires AFNetworking. So I installed the cocoapods, and included it in my static library

However, when I include my static library into another project that also needs AFNetworking, and I run the project, I get a duplicate symbol error for all the common classes.

What is the best approach to avoid a conflict like this?

Upvotes: 1

Views: 734

Answers (2)

art-divin
art-divin

Reputation: 1645

The best approach is not to include any 3rd-party libraries / frameworks inside your framework.

Here's Apple reference regarding "How to build your own framework". There are a lot of guidelines that will help to build a good framework that will not cause any errors if you'll link it to any project.

AFNetworking is a framework that is application specific, so you should not include it into your framework. You may weakly link your framework to AFNetworking, but you should avoid strongly linking it.

Here's resource regarding weak linking.

For example, if you are using 3rd-party library, you should specify that your framework require user to add dependencies in order to use it. Here's an example how AFNetworking does it. Also, check this framework, and it's requirements regarding dependencies in its specification.

Upvotes: 1

bachonk
bachonk

Reputation: 3954

The best way to do this (in my opinion) is to underscore the class names to keep them unique.

So let's say your library is called XYLibrary. It will have a bunch of AFNetworking files like AFNetworking.h and AFNetworking.m. Rename those to XY_AFNetworking.h and XY_AFNetworking.m. This should hopefully keep them unique from other instances of that library in another project.

You should do this for any other 3rd party libraries as well.

Upvotes: 0

Related Questions