iOS Universal Framework Mk 3
An XCode 4 project template to build universal (arm6, arm7, and simulator) frameworks for iOS.
By Karl Stenerud
Why a Framework?
Distributing libraries in a developer-friendly manner is tricky. You need to include not only the library itself, but also any public include files, resources, scripts etc.
Apple's solution to this problem is frameworks, which are basically folders that follow a standard structure to include everything required to use a library. Unfortunately, in disallowing dynamically linked libraries in iOS, Apple also removed iOS framework creation functionality in XCode.
Xcode is still technically capable of building frameworks for iOS, and with a little tweaking it can be re-enabled.
Kinds of Frameworks
The most common kind of framework is the dynamically linked framework. Only Apple can install these on an iOS device, so there's no point in building them.
A statically linked framework is almost the same, except it gets linked to your binary at compile time, so there's no problem using them.
An embedded framework is a wrapper around a static framework, designed to trick Xcode into seeing the framework's resources (images, plists, nibs, etc).
This template system will build static frameworks and embedded frameworks.
Installing the Template System
To install, run the install.sh script.
Now restart Xcode and you'll see Static iOS Framework under Framework & Library when creating a new project.
To uninstall, run the uninstall.sh script and restart Xcode.
Creating an iOS Framework Project
Start a new project.
For the project type, choose Static iOS Framework, which is under Framework & Library.
Optionally choose to include unit tests.
Add your classes, resources, etc with your framework as the target.
Any header files that need to be available to other projects must be declared public. To do so, go to Build Phases in your framework target, expand Copy Headers, then drag any header files you want to make public from the Project or Private section to the Public section.
Building your iOS Framework
Select the Universal_Framework scheme (any of its targets will do).
Build. It will build the framework for arm6, arm7, and simulator, then merge them together into a universal framework.
The finished product will be in your standard build location as (configuration)-universal/(your framework).framework. The "Run" build configuration in the "Universal_Framework" scheme controls which configuration to build the universal framework from.
It will also build (your framework).embeddedframework. This contains the normal framework, but also contains an extra reference to any resources included in your project. XCode won't look inside static frameworks for resources, so if you're going to include resources, you must distribute the embeddedframework version.
Using an iOS Framework
iOS frameworks are basically the same as regular dynamic Mac OS X frameworks, except they are statically linked.
To add a framework to your project, simply drag it into your project. When including headers from your framework, remember to use angle bracket syntax rather than quotes.
For example, with framework "MyFramework":
Headers Not Found
If Xcode can't find the header files from your framework, you've likely forgotten to make them public. See step 5 in Creating an iOS Framework Project
No Such Product Type
If someone who has not installed iOS Universal Framework in their development environment attempts to build a universal framework project, they'll get the following error:
target specifies product type 'com.apple.product-type.framework.static', but there's no such product type for the 'iphonesimulator' platform
Xcode requires some modification in order to be able to build true iOS static frameworks (see the diff files for details), so please install it on all development machines that will build your static framework projects (this isn't needed for users of your framework, only for builders of the framework).
The selected run destination is not valid for this action
Sometimes Xcode gets confused and loads the wrong active settings. The first thing to try is restarting Xcode. If it still fails, Xcode generated a bad project (this can happen with any kind of project due to a bug in Xcode 4). If this happens, you'll need to start over and create a new project.
The first time you build your framework target (not the Universal_Framework target), XCode may complain about missing folders during the linking phase:
ld: warning: directory not found for option '-L/Users/myself/Library/Developer/Xcode/DerivedData/MyFramework-ccahfoccjqiognaqraesrxdyqcne/Build/Products/Debug-iphoneos'
If this happens, simply clean and rebuild the target and the warnings should go away.
The first incarnation. It used a bunch of script hackery to cobble together a fake framework. It exploited the "bundle" target, setting its type to a relocatable object file.
This version took advantage of the template system to do most of the work that the script used to do. Everything (including the script) was embedded in the template.
This version does away with the "relocatable object" hackery and builds a true static framework, with all the abilities of an OS X static framework. This solves a number of linker, unit testing, and workspace inclusion issues that plagued the previous hacky implementations.
It also includes the concept of the embeddedframework, which allows you to include resources with your framework in a way that Xcode understands.
Josh Weinberg also added some tweaks to make it build in the proper build directory with scheme-controlled configuration, and behave better as a subproject dependency.
It now requires some small modifications to Xcode's specification files in order to support true static frameworks, and thus comes with an install and uninstall script.
Copyright (c) 2011 Karl Stenerud
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in the documentation of any redistributions of the template files themselves (but not in projects built using the templates).
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.