| How to (almost) create your own iPhone OS framework |
| Written by Peter Bakhyryev |
| Tuesday, 25 November 2008 03:52 |
|
As of iPhone SDK version 2.1, it doesn't seem to be possible to package your own code into an iPhone OS framework. Messing around with OS X 10.5 framework build options didn't yield any useful results for us. The closest we got is a static library built for each platform (iPhone OS and the Aspen simulator), bundled up into one file (using a command line tool called 'lipo') and a set of header files that you would reference in order to use the library. Setting: You have several implementation files with the corresponding header files that you'd like to reuse in another iPhone app project. Step 1: Create static libraries for iPhone OS and iPhone Simulator.You need to create one "static library" build target for each platform in an empty project. Create a new "Window-Based Application" project for iPhone OS. Delete all source and NIB files and remove all references to frameworks that your library won't be using. Keep the .pch file. Remove the existing build target. At this point, your project should look something like this:
In order to bundle lib files for different platforms into one, we need to build them separately first. To do that, create 2 new "Static Library" build targets, we'll call them 'PseudoFramework sim' and 'PseudoFramework dev' for Simulator and device, respectively. Add your library source and header files. In the build settings for target 'PseudoFramework sim', click on 'Base SDK' and select 'Simulator - iPhone OS'; 'PseudoFramework dev' should be using the 'Device - iPhone OS' Base SDK. At this point you should be able to build separate .a files by selecting "Active SDK"->"Use project settings" from the dropdown box on top, selecting your desired configuration (debug or release) and right-clicking on each target and selecting "Build" from the menu. At this point, your project should look something like this:
Step 2: Merge library files for different platforms into one.There is a command line tool called 'lipo' that allows you to combine different .a files into one. It comes for free with the SDK. Using a custom build target, we can execute that command after everything got built by the compiler/linker. Now, create a new "Shell Script" build target, we'll call it 'Build ALL'. This will allow us to call the 'lipo' tool during build time. Drag the other static library build targets (not the actual .a files) into the new target - this will make sure that our lib files get built before we bundle them up. Expand 'Build ALL' and double-click on "Run Script". In the window that opens up, paste the following shell script: # remove existing product lib file, just in case In the dropdown box on top, select 'Use project settings' for Active SDK and 'Debug' for Active Configuration. Now, when you right-click on 'Build ALL' target and select "Build", both 'sim' and 'dev' lib files should be built along with the combined 'libPseudoFramework-Debug.a' file, which you should be able to find under 'build/Debug-iphoneos' folder in your project directory. This is the debug version of the library, with debug symbols and all that stuff. If you change Active Configuration to Release and run 'Build ALL' again, a release version will show up as 'build/Release-iphoneos/libPseudoFramework-Release.a'. These are the library files that you can now use in other projects. Step 3 (optional): Create references to the resulting .a files in the PseudoFramework project to make it easier to use.Create a new group in the project, call it Dist. Right-click on it and select "Add existing files...". Add both libPseudoFramework-Debug.a and libPseudoFramework-Release.a files to it. Now, when you need to add the library to another project, you can just drag and drop it from XCode and don't need to open Finder and locate the files every time. Your project window should look something like this:
Step 4 (optional): Build libraries with different versions of the SDK.If you plan to use same code with different versions of the iPhone SDK, you can build different static libraries, one for each version. To do that, create another set of build targets with a different SDK version in the 'Base SDK' field. You will want the names of your lib files to be different in order to avoid files being overwritten. I'd suggest appending 20 or 21 to file names to indicate that something was build for iPhone OS versions 2.0 and 2.1 respectively. Don't forget to modify the shell script to account for different file names. It should now look roughly like this: # remove existing product lib files, just in case Another screenshot of the project window:
|



