Difference between framework and static library in xcode4, and how to call them

Leonardo picture Leonardo · Jun 5, 2011 · Viewed 53.7k times · Source

I am quite new to xcode and objective-c. I want to ask a very basic question.

I saw that when "linking binary to libraries" in project settings, there are differences about framework and libraries, imported from other projects in workspace.

First question, why is there a framework? And why is there a library? Cannot my library be a framework?

And then, from a .h file, how can I call classes from my imported static library?

I suppose there must be a prefix, but I couldn't find it. Neither nor "ProjName/Myclass.h" are working.

Please, be as specific as you can.

Thanks

Answer

Luke Redpath picture Luke Redpath · Jun 17, 2011

The biggest advantage a framework has over static libraries is that they act as a neat way of packaging up the compiled library binary and any related headers. They can be dropped into your project (just like the SDK's built-in frameworks like Foundation and UIKit) and they should just work (most of the time).

Most frameworks contain dynamic libraries; frameworks created in Xcode using the Mac Framework template will create a dynamic library. The iPhone does not support dynamic frameworks which is why it has become common for reusable libraries of iOS code to be distributed as static libraries instead.

Static libraries are fine, but they require a bit of extra work on the part of the user. You need to link your project to the library and you need to copy the header files into your project or reference them somewhere by setting the appropriate header search paths in your build settings.

So: in summary, my opinion is that the best way of distributing your library is as a framework. To create a "static" framework for iOS, you can essentially take a normal framework and replace the binary with your compiled static library. This is how I distribute one of my libraries, Resty and is how I intend to distribute my libraries in the future.

You may want to look at the supplied Rakefile in that project (in case you aren't aware, Rake is Ruby's equivalent of Make). I have a handful of tasks for compiling my project (using xcodebuild) and packaging them as a static framework for iOS. You should find this useful.

Alternatively, you may wish to use these Xcode 4 templates for creating an iOS framework.

Update 9 Dec 2013: this is a popular answer so I thought I'd edit to say that my first choice for library distribution has changed. My first choice for any third party library as either a consumer or producer is CocoaPods. I distribute my libraries using CocoaPods and offer a precompiled static library with headers as a fallback option.