For some reason Apple reserves the ability to create iPhone frameworks for their own use. They are a very useful ability to have because they package headers together with a universal binary. „Universal“ meaning that you get binary code for multiple platforms rolled into a single file. Frameworks are also dynamically loaded which means that less binary code fills up your memory as only stuff is put there that is actually needed. But alas, no frameworks for us. Until now I am selling my components in the Dr. Touch’s Parts Store as source code which at the same time means that I could not provide demonstration versions to try out in your own code. If you have the code, what incentive is there to pay the price? It has been suggested to me by several people independently to make a static library to solve this problem. Could be time- or otherwise limited, but then people could try the parts out and see how well they fit with their own work. Also next to still offering access to my repositories at full price, I could be selling indidual versions without right to upgrade to get started at a substantial discount. Later if you find that the component really IS worth what I am asking for, you could upgrade to the lifetime upgrade plan via the SVN repo. Another reason to figure out this Static Library Magic is so that I can bundle my own utility classes that I keep adding to and improving in a central place. This gives me the choice of adding an external SVN reference or to simply copy the static library into my new projects. In this article I am describing how to create a universal static library for both Intel and Arm architectures, how to glue them together into a single file and how to add it to a new demo project. Step 1 – Code for a new utility static library Start out by creating an iPhone static library project. So that there is actually code inside our static library we create a class extension for NSURL to give us a dictionary of all the parameters passed in a URL. This is quite useful when analyzing the parameters from the applicationion:didFinishLaunchingWithOptions if your app is launched via its URL scheme. NSURL+DT.h @interface NSURL (DT) // dictiary which contains param = value for each parameter – (NSDictionary *) dictionaryOfParameters; @end NSURL+DT.m #import "NSURL+DT.h" @implementation NSURL (DT) – (NSDictionary *) dictionaryOfParameters { NSString *paramName; NSString *paramValue; NSMutableDictionary *tmpDict = [NSMutableDictionary dictionary]; NSScanner *scanner = [NSScanner scannerWithString:[self query]]; // NOTE: This cannot deal with values that contain & or similar HTML entities while (![scanner isAtEnd]) { [scanner scanUpToString:@"=" intoString:&paramName]; [scanner scanString:@"=" intoString:nil]; [scanner scanUpToString:@"&" intoString:&paramValue]; [scanner scanString:@"&" intoString:nil]; [tmpDict setObject:paramValue forKey:paramName]; } return [NSDictionary dictionaryWithDictionary:tmpDict]; } @end Drag the header into the „Copy Headers“ section of the target. Drag the implementation file into the „Compile Sources“ section. Contrary to a regular iPhone app target you have to do that manually so that your static library actually contains anything to compile. Also note that the build steps differ from those for an app target. Step 2 – Set up the targets Your default Cocoa Touch static library comes with a single target. We want to be able to build for multiple targets with different architectures. So first we change the current target’s base SDK to iPhone SDK 3.0. This will build it for the arm6 and arm7 platforms. Rename the target by appending Dev to the target name so that we know this is the one set up for device. Next we duplicate the target by right-clicking on it and „Duplicate“. Rename the second target by appending Sim to the name to signal that this is the one built for simulator. We also set the base SDK for the Sim target to be the same version but for simulator. This causes the build to be made for the i386 platform. The SDK you choose in the upper lefthand box overrides the set base SDK for your targets. So if you want the selected SDKs to be built for you have to set it to „Use Base SDK“. If this is set then building the individual targets obeys the individually set base SDK. Let’s try building both targets. There is no Build-All option yet, so we build both targets one after the other. It’s no problem that they have the same output file name because due to the different SDK they end up in different subfolders of your build folder, Debug-iphonesimulator and Debug-iphoneos. If you like you can get them renamed by changing the product name of the targets, but that’s optional. The following shows the layout of your project … Continue reading Universal Static Libraries
Copy and paste this URL into your WordPress site to embed
Copy and paste this code into your site to embed