How should i handle a dependency in my static library? - ios

I have written a static library which interfaces with a web service for internal use across multiple projects.
This library uses AFNetworking heavily, but so do the projects which will use the library. If I include AFNetworking in the library.
I get duplicate symbol errors, but if I remove AFNetworking from the library, it won't compile.
How should I address this dependency ?
Edit: My project is in a GIT repository. Whatever I do, other developers should be able to pull the project and have it compile successfully (unless I am wrong and I shouldn't be that way).

The easiest solution is to rename AFNetworking Classes.
Go to each .m file of afnetwork, click on class name, then Edit->Refactor->Rename
.There are around 10 files. It wouldn't take you long.
PS: A good practise is to prepend your library initials, for example if your library name is MyLibrary, you can rename AFNetworking classes like
MyLibAFNetworking... MyLibAFHTTP....

Use Cocoapods, you can add the dependency for AFNetworking and it will automatically add the library and everything will work like a charm. https://cocoapods.org/

Copy the files of AFNetworking project and add them to your project as files of your library. In other words,
Right click on any AFNetworking file in your project and do Show in Finder.
Select all the AFNetworking files.
Drag them to your Library project and then drop them.
When the pop up appears make sure Copy Items if needed is check marked.
Cheers!

Related

iOS: Can i create a static library with all classes (.h & .m) as public?

I have a bunch of classes that i want to share with others so i thought of creating a static library but i don't want to hide my code. I want to allow them to put change in the library if they want to.
I can share a folder of files but i want to merge all the files into a single package so it's easy to manage and maintain.
Static library generally shows only header (.h) files to users but is it possible to make implementation (.m) files also visible?
I've one more static library which is a dependency for this one. Can i put one static library inside another? or i have to add both files in my project?
I didn't do this library stuff before so am i going in a right direction? or is there any more appropriate way in iOS that can fulfil my requirement?
Thanks!
Ship the code as a framework and instead of sharing a folder, turn the library into a regular project on GitHub or anywhere else. Add what’s needed to install the library through CocoaPods or Carthage. Make proper releases with semantic versioning, write a proper Changelog.
It’s a lot of stuff to go through for the first time, but it’s a good practice that will pay off. See my simple goo.gl client for an example of such library.

Best way to create a library project in Xcode for publishing on git or CocoaPods

I just want to know the best possible way to create a library project, which contain the resources that the library code uses—such as image files or .xib files. Also if any library needs other library (like-AFNetworking) then how to manage the dependency of other library.
In short, something like Facebook sdk.
you can use cocoapods to create a static library ,which contain some resources ,librarys,dependencys. the https://guides.cocoapods.org/making/using-pod-lib-create.html maybe help you.

iOS Framework build: what is the best practice to link with third-party libraries?

I'm build a Framework for iOS and my framework has AFNetworking as dependency.
So what is the best practice to include AFNetworing? Reading other posts (specially this question here) i came up with three options:
Copy all the .h.m files from AFNetworing in to my project and compile my framework with it. But according to this, it will possible cause a situation where some third part developer using my Framework are already using AFNetworking and will get a compile-time error that some class is declared twice.
Use CocoaPods and link AFNetworking to my Framework. This is causing this error: -lPods is not an object file (not allowed in a library).
Build something like the Aeris SDK where the third part developer using my Framework will be responsibly to add AFNetworking to their project.
I think that option 3 is the best but i don't know how to do that. How can i dev my framework calling AFNetworking classes/methods but do not include on the final framework product?
Thanks!
That's a very bad practice to use third party library in you library.
Ideally you should avoid doing that.
But if you really need it, you can define and add prefixes to class names.
Refer this article Avoiding dependency collisions in iOS static library managed by CocoaPods
Ok. I decided to go with the option 3.
A just added all the header files from any third-party lib to the my framework project (you can also add the .m files but not include them on the static library target).
And a i also documented everything, so developers using my framework will know what are the dependencies and how to include/install them on their own projects (including third-party lib versions, CocoaPods support, etc).
I decided not to go with option 1 because that will cause some situations where a project will have two copies of the same lib compiled on the final app. Even if a change the namespace for the libs on my framework code (to prevent "duplicated symbols" errors) that will still cause some other possible problems like an APP with larger size, possible bugs related to two or more instances of the same lib running together, etc...

How to add AFNetworking to XCode app

I'm relatively new to XCode and iOS. My background for understanding how to add libraries/dependencies to a project is in the world of Java and Maven.
I need to add AFNetworking to my project and have been unable to do so successfully.
First, I tried going to my project -> Build Phases -> Link Binary With Libraries, clicking the "+" button, and then search for AFNetworking from the list. It wasn't in the list.
So, I figured I'd have to add it using the "Add Other..." option. Which means, of course, I'd need to download AFNetworking somehow. I'm not sure I was able to download it in the correct form -- it seems all I could get was the AFNetworking project itself, rather than a framework (if I understand correctly, frameworks are analogous to jars? Can you generate a framework by building an iOS project in a certain way?). I downloaded it from their GitHub page. (I also tried their "Getting Started" guide, but it had me using CocoaPods, which I couldn't get to work right and I'd rather avoid it for now.)
I've tried adding project files and folders from AFNetworking just to see if they worked (but of course they don't -- I think I need some sort of .framework file).
Bottom line: I can't seem to find a way to add AFNetworking to my project. How can I do it?
It sounds like you got the first part down, which is downloading it from the GitHub page.
When I follow this tutorial, it shows how they imported AFNetworking into their own sample project (that is, you'll see a "AFNetworking" group in the list of files and folders along the left side of your Xcode window).
Once AFNetworking is building along with the rest of your code in your project, you should be able to call into the various AFNetworking API's from your code. You'll need to make certain to add in the appropriate "#import "AFNetworking.h"" lines into your source code files from which you'll be calling AFNetworking.
Use pods, than you wont have to upload AFNetworking to your github project, and there is no worry about wrong linkers etc.
Use pods: install pods
Instalation AFNetworkign with pods: install AFNetworking
I believe if you download their code you can generate the .a file using the process in this stack overflow answer: (obviously replacing the project they are getting from gihut with the project you found)
How to create framework from Xcode 4 project?
Use this Manager to help you use AFNetwork.

How to import/link shared xCode project (and be able to edit shared project from any client)?

I don't want to compile shared project to any kind of library. I just want to use existing classes like they would integral part of client project.
I want to edit/develop/improve the shared project from any client project that use it.
I've ran trough google, but couldn't find the simpliest/more convenient approach.
More simple:
I want drag and drop shared code project, and be able to edit it anywhere. How?
There are two options: Use a static library, or include the source files directly in your project.
If you don't want to use a static library (which IMHO is the preferred method for iOS and integrates well with Xcode), you could just add references to the shared classes either as file references or as a folder reference.
Either way, the build toolchain will link everything together into one Mach-O binary, which is the only way to deliver code for the iOS.

Resources