Shared Code for iOS
We all have multiple iPhone, iPad (iOS) apps that we are creating. We all have a collection of classes, methods and categories that we have become accustom to having access to.
What I want is to have multiple apps that are all individual applications in their own source control repository. I also want to have one (or more) source control repository that contains code that ALL my projects can use. I want to be able to easily edit (fix bugs, add enhancements) the shared code while in the middle of working on one of my apps.
Here is what I ended up with:
/Users/myname/dev/app1
/Users/myname/dev/app2
/Users/myname/dev/app3
/Users/myname/dev/sharedLib
/Users/myname/dev/sharedLib2
Each of the above folders is a separate GitHub repo. (Yea!)
STEP 1: Create my first library of shared code
- Create "sharedLib" Xcode project (You can name it anything you like)
- File | New | Project...
- Select "Framework and Library" under iOS
- Select "Cocoa Touch Static Library"
- "Next"
- Product Name: "sharedLib"
- (everything else should be filled out, like Organization Name, Use ARC ...)
- "Next"
- Select your folder where all your projects and shared libraries will live (/Users/myname/dev in my case)
- "Create"
- Click the "Run" button to build the library (which is empty for now)
- The "Library" of shared code exists and is ready to use in your "apps"
- For testing purpose, add at least 1 class to the library and re-build it
STEP 2: Prepare your library for easy use by other apps
- We will be using the default .h file that was created with your library as the "public access" to all classes, methods and categories in your library.
- Open the .m file that was created with your library ("sharedLib.m") and comment out the simple content. Do the same with the .h file.
- #import your classes in the .h file (#import "MyFunctions.h")
STEP 3: Add the library into an app
- Either use an existing app, or create a new app (in Xcode, duh)
- I'll assume the app is in a sibling folder to the shared library project
- Open a Finder window to the library project
- Right+Click on any file in your library project and select "Show in Finder..."
- Close the Xcode project for the shared library (You can only have a project open "once", and when it is shared with an app, the library will be open.)
- Add the "sharedLib.xcodeproj" to your app
- Have Xcode and Finder both on the screen at the same time
- Drag the sharedLib.xcodeproj file onto Xcode
- Drop it in the project window, at the bottom (You should see a cute little blue line at the bottom of your project list, probably below below a folder named "Products")
- (If you already have a sub-project added to your project, you can drop the xcodeproj file on the top-item -- the actual project, the one in blue)
STEP 4: Configure you app to use your library
- Note: We still have your app open in Xcode, and the library is shown in the project window
- Add the library as a "Build Dependency"
- Select the project (top item in the window)
- Make sure the "TARGETS" window has the app selected
- Select "Build Phases"
- Expand "Target Dependencies (0 items)"
- Click "+" (to add a new dependency)
- You will see a tree with about 5 items shown
- Select the one near the bottom called "sharedLib" (not the "tests" version) with the house icon
- Click "Add"
- Add the library as a "Library"
- Still in the same spot as you were ("Build Phases")
- Expand "Link Binary with Libraries (3 items)"
- Click "+" (to add a new library -- your new library)
- At the top of the dialog, you should see "libsharedlib.a" -- select it
- Click "Add" (Note: it may show in red)
- Add the library as "Searchable Headers"
- Still in the same spot as you were ("Build Phases")
- Switch to "Build Settings"
- Make sure you have "All" selected (not "Basic")
- Scroll way down to "Search Paths" (it is about in the middle)
- Find "Header Search Paths"
- Double-Click on the right-hand-side of "Header Search Paths" row
- Click "+" to add a new search path
- Type: ../sharedLib/sharedLib (This is the *relative* path to the library code folder)
- Note: if you use sub-folders in your library, change "non-recursive" to "recursive"
- Set linker options
- Still in the same spot as you were ("Build Settings")
- Scroll down to "Linking" (a couple above "Search Paths")
- Find "Other Linker Flags" (about the middle of this section)
- Double-Click on the right-hand-side of "Other Linker Flags" row
- Click "+" to add a new linker flag
- Type: -ObjC
- Note: This option will make categories in your library available in your app
STEP 5: Code up your app
- #import "sharedLib.h" in your .pch of your app
- Code your app, build, run, repeat
- If, and when, needed: code in the library, build app, run, repeat
- Live happy
- Build another app using the exact same library
- Make millions
No comments:
Post a Comment