iOS Frameworks

Guides | Tutorial By 6 years ago

After developing iPhone applications for a while you tend to develop some ‘must have’ pieces of code that you use on all of your apps. Being able to share the code on multiple projects is great but having the code duplicated among many projects is very annoying, especially if you make an update, you would need to change all files to reflect the changes. Of course you could just reference the same source files from all your apps, but this can cause conflicts when other developers try to pull the project from the repo as the path to the source files has probably changed. It’s time to make a Framework, a compiled binary of your source that can be included in your projects, keeping the source in one place.

Now you may have seen the Framework option under the Mac OS X section when creating a new Xcode project, but where’s that darn iOS one? There is the static library option but a Framework is a very convenient way to group your headers and binaries. Unfortunately there isn’t one, but there are templates available that will do the exact same thing. I personally use Karl Stenerud’s iOS Universal Framework template from GitHub. It will compile both iPhone simulator and iPhone device binaries into one, so you no longer need to manage two binaries (if you have used static libraries in the past).

Download the project and open the “Real Framework” folder. Make sure Xcode is closed and execute the install script from your Terminal. Reopen Xcode and you should see a new Framework project for iOS.

When creating a Framework you need to remember there is no main entry point to your code (no main function required). The idea of the Framework is to be used by another app to use it’s shared classes and methods.

Go ahead and add all your headers and implementation files. Open the project’s properties and under the Framework’s target (the one that looks like a toolbox) select the ‘Build Phases’ tab. Look for the ‘Copy Headers’ section and drag all the headers you want people to have access to up into the public section. These headers will be included in your built Framework. Make sure your build scheme is set to “Universal Framework” and you are building for release and then do a build. In the products folder in the file inspector right click on your framework and show it in the Finder. Go back one folder to the products folder and look for the “Release-universal” folder. This is where the simulator/device Framework will be built to. It’s important you use this one otherwise the Framework will only work on either the simulator or the device, depending on which one you built for. In this folder you can take your Framework and add it to your iPhone app.

An important thing to remember (which had me running around in circles trying to figure it out when I made my first Framework) is that if you are using categories you MUST add something to each new project you create that will include the Framework. In the project’s ‘Build Settings’ under the ‘Other Linker Flags’ make sure you add “-all_load” or else the compiler wont include any category code and your app will crash when you try and use them.