3

I'm diving into iOS development and I plan to build a few basic apps that make use of my own reusable code. As I update the files that contain my reusable code, I'd like those updates to be reflected in all the Xcode projects that use them. What are some ways I can do this?

Thanks in advance for your help!

5 Answers 5

4

There are a few ways you can do it.

If your "reusable code" is separated into libraries, you could package it up as individual projects (that compile to static libraries) and then link them into your main project (that compiles into an executable). A bit tricky, but it's the "proper" way. (Not as flexible or easy as Visual Studio's solution/project paradigm, but that's a rant for another day.)

You could also just drag-and-drop a directory containing the source files into the Project window directly, either using "Create Folder References" or "Recursively create groups". (Either way, you don't want to use the "Copy items" checkbox at the top if you want only one copy of the source files.)

I've done all three.

You may want to start with the latter -- group your "reusable code" into a folder somewhere (so there's only one copy) and then drag-and-drop that folder into your project using "Recursively create groups". One warning: if you do that in multiple projects, and sometime later you want to remove it from the project, don't use the option to send it to the trash, or your other projects could get messed up.

4
  • One issue with this technique is that source code stored in different directory trees is not managed by the Git repository of the app I am developing. How do you work around this?
    – radven
    Mar 4, 2011 at 23:03
  • @radven Perhaps you can describe the problem you are envisioning so your question is a little more clear Mar 4, 2011 at 23:44
  • When I check out version 1.3 of my app, it is important that all the reusable code in other folders match what was used to build version 1.3, even if they have been updated separately. Managing this is the part that confuses me.
    – radven
    Mar 8, 2011 at 14:52
  • 1
    @radven - take a look at git submodules - you have a git repository for each project and then an extra one that contains the shared code - if you add this to each project's repository they can all be pointing at a specific (and different) version of the shared code. Nov 3, 2011 at 16:31
1

This sounds like something you could use Git and branching for.

1

Create a static library project, then use the interproject dependency feature of Xcode to build them in the correct order and link the app with the static library

0

One solution I've thought of is to build a static library project, but only use it just to test compile the reusable code in that library. Then symlink the reusable source code subdirectory of that library project into all other projects that use that code. That way all the source code is available in any project, and any changes I make in the reusable code will be reflected everywhere else immediately (no copy step). And of course, use a good source code revision control methodology on that subdirectory, so that I can version it, compare changes, revert, etc.

0

the proper way is to create a library. in iOS, you're restricted to static libraries, but dynamic libraries and frameworks are other common possibilities (for example, if you're targeting OS X).

to configure this (using libStatic as a name for the static library, and App as the app in this example):

1) create the libStatic project, and configure it to compile the files you choose

2) add libStatic.xcodeproj to the App.xcodeproj. this adds the project symbols, indexes and references to the App project.

(the remaining steps all take place in App.xcodeproj)

3) double click the target which depends on liStatic

4) navigate to General tab

5) click '+' under the dependencies list, and add the libStatic target to the dependencies. it is important that you create this association/dependency so the libStatic target will be kept up to date as you develop in App (it will be built before App).

6) navigate to the libStatic project reference in the groups & files tree. if necessary, click (expand) the disclosure triangle to view the products of libStatic.

7) drag the appropriate product from the libStatic project reference into the App target's link stage.

8) clean, build, run

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.