Saturday, August 25, 2012

Using Static Libraries in Apps in Xcode

I looked at a ton of misinformation and confusing information on StackOverflow to try to do this. Between my ADD and possibly outdated answers, I decided to spin my own. It's got some manual stuff in it, but it works. Here's what the solution looks like

Project 2 is a static library. Project 1 is your app.
  • Close Project 2. Yes, you have to do this.
  • Using drag and drop or add files, add Project 2 to Project 1. 
  • In the root directory for Project 1, add a soft link to the directory where Project 2's sources are kept. So in this example, I'm in /myMillionDollarProjects/UsingConfusionUtil and I type

    ln -s /myMillionDollarProjects/ConfusionUtil ConfusionUtil
  • Exclude this link from git by adding the name of your soft link to your .gitignore (in this case, ConfusionUtil).
  • In User Header Search Paths for Project 1, add the name of the soft link (in this case, ConfusionUtil). 
  • Check the box to the left of that which means "recursive." This just adds a forward-slash followed by two asterisks to the name (so in this case, I see ConfusionUtil/**).


  • In Build Phases for Project 1, under Link Binary With Libraries, add the .a for Project 2. 
  • In your Other Linker Flags, add -ObjC with the leading dash (otherwise categories won't work)
  • Try to reference something from Project 2 in Project 1. Don't just compile, but also run to make sure it's all correct.

Special Note: You cannot add nibs this way, nor is there a concept of a bundle on iOS. You have to add the nibs as individual files, although I'm going to investigate making a fake app target and adding them that way.

Note: If you need to use static libs, you can figure all these issues out and more, but personally the distribution provision cert issues proved to be too much.


No comments: