iOS Universal Framework
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.
It is, however, possible to make a statically linked framework with the right magical incantations. This project template provides that magic.
Installing the Template
Just copy Static iOS Framework.xctemplate and Static iOS Framework Test.xctemplate to your XCode templates folder ~/Library/Developer/Xcode/Templates/Framework & Library (create the folder if it doesn't exist yet).
Now restart Xcode and you'll see Static iOS Framework under Framework & Library when creating a new project.
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 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 project directory under build/Release-universal/(your framework).framework
It builds in this folder because the command line xcodebuild doesn't use the new derived data folder that XCode 4 uses.
Using an iOS Framework
iOS frameworks are basically the same as 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":
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 selected run destination is not valid for this action
Sometimes Xcode doesn't construct the project properly, and so when you attempt to build, it will bring up this error. As a preventative measure, try building your framework immedeately after creating it. If you get this error, Xcode built the project incorrectly.
If Xcode builds a bad project, you'll have to delete the project and start a new one. This error only occurs during the initial construction of the project file.
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.