Xcode does not allow you to build dylib for iOS. App will be rejected if it’s not single binary. But I have an application that has plug-in architecture to load optional modules. I just want a quick prototype to prove concept before fully port it to iOS. It’s faster to do if dylib could simply work. So, this post shows how to build and use dylib but be aware it won’t be approved to App Store. (Tested with Xcode 3.2.4 on 10.6.4)
1. Open these files in the Property List Editor: “/Developer/Platforms/MacOSX.platform/Developer/Library/Xcode/Specifications/MacOSX Product Types.xcspec” and “/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Xcode/Specifications/iPhone Simulator ProductTypes.xcspec”
2. Locate the item in the “MacOSX Product Types.xcspec” that has the product type “com.apple.product-type.library.dynamic” and drag it to the “iPhone Simulator ProductTypes.xcspec”.
3. Open “MacOSX Package Types.xcspec” and “iPhone Simulator PackageTypes.xcspec” found at the same places.
4. Locate the item in the “MacOSX Product Types.xcspec” that has the package type “com.apple.package-type.mach-o-dylib” and drag it to the “iPhone Simulator PackageTypes.xcspec”.
5. Repeat the steps for the “iPhoneOS.platform” and relaunch Xcode if it was running.
Now, lets build a dylib. Start out with the “Cocoa Touch Static Library” Templete. That should included the Foundation.framework in the project. Here are the changes I made on top of the templete to build dylib.
1. Open the file “project.pbxproj” (found inside the Xcode project file bundle) in a Text Editor. Search for string “producttype”, change it’s value to “com.apple.product-type.library.dynamic”;
Now, open the project with Xcode, go to Project->Edit Project Settings
2. “Installation Directory” set to @executable_path/ because I plan to put the dylib in the same directory as the app’s executable.
2. “Mach-O Type” set to Dynamic Library
3. “Executable Extension” set to dylib
4. “Executable Prefix” set to empty
5. Add one or two simple methods to the library and build it.
Now, create an app to test it. This time, I choose the “View-based Application”. Hook up a UIButton and a UILable to call the lib and showing return message. You can download the complete project TestApp and play with it.