iPhone Static Framework

Problem with iPhone code sharing

As we all know Apple has not left developers opportunity to create frameworks for iPhone. However, there are a lot of situations when code has to be shared between different projects or even distributed to others developers.

There are a few approaches for the code sharing or distributing. Firstly, code can be shared/distributed as a source code. Although this approach is quite straightforward and XCode provides a good mechanism for including XCode project to another project, developers have to do some extra steps such as setup dependencies and path for headers. On the one hand it seems quite simple for the experienced developers, on the other it might be a problem for the new developers .

Second approach is to distribute code as a static library. This approach is well known and is used widely in Linux/Unix development thus a lot of developers are familiar with it. However, there are some disadvantages of using it in iPhone development which relate to dynamic nature of Objective-C. The problem is that static libraries are normally used for C/C++ code which has neither properties nor categories and so when, for example, Objective-C category is wrapped in static library a developer has explicitly to set additional linker options:


If developer forgets to set the options mentioned above he will get runtime error “unrecognized selector sent to instance”

Static Framework
The most suitable alternative, in my opinion, is a ’static framework’. The idea behind is to force compiler to believe that it uses normal iPhone framework whereas shared libraries are substituted by static libraries. Thus if you have the code you want to share as a static library it is quite easy to transform it into a framework. Afterwords such framework can be added to a project without any extra efforts from the developer’s side.

How to create a Static framework
Since there is no option to build frameworks for iPhone in XCode I recommend using a static framework. To build it into XCode MacOS framework template should be used. Static framework has the same structure as a normal framework. Only a few extra steps have to be done:
link together static library for iPhone Simulator and iPhone Device
add file from the previous step to framework bundle

To make it easy we will write a shell script which does all the work. We will use a lipo utility which is distributed with XCode installation. This utility allows to link a few libraries into one file. Afterwords this file will be copied to the framework bundle and appropriate links will be created.

lipo \”${BUILD_DIR}/${CONFIGURATION}-iphoneos/libDev.a” “${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/lib Sim.a” -create -output “${FRAMEWORK}/Versions/Current/${PRODUCT_NAME}”cd “${FRAMEWORK}” && ln -sf “Versions/Current/${PRODUCT_NAME}” ./

This script should be added into a project as a build phase script through the project menu. As a result we get a static framework containing code which now can be easily shared.


Mac OS Frameworks

lipo man page

Static Framework

May 10th, 2010 - Posted by Nick Black in api, for developers, iPhone |