( jur - nay - sic ) Entrepreneur / Software Engineer

How To Auto-Increment Your iOS Build Number

UPDATE: The scripts on this page have been updated to auto-update the version number as well and can be found here.

Things do not always go smoothly in Xcode, as I have mentioned before.  Since I have had to do many builds with small tweaks and upload them to TestFlight to test my app, I started to get lazy with my build numbers.  As such, I decided that it was time to figure out how to automate incrementation of it.

There is a lot on the web documenting different ways to this.  Many of them seem overly complicated.  The two most helpful sources for me were by Fredrik Olsson and, of course, StackOverflow.  With these posts and their comments I was able to work up a solution that met my requirements:

  1. Be as simple as possible.
  2. Only increment on release and ad hoc builds.

A lot of the examples floating around are using a script that calls a script.  I do not to have to track another file for something like this, and I do not want to be messing with multiple languages in the same project.  I want it in Xcode, in the project settings, period.  And a lot of the default examples only show how to increment but not how to control when it happens.  Maybe I am picky, but I want the build number to increment on significant builds but not when I build the project seventeen times to debug something.

So here is the script that I came up with from Fredrik’s example and the comments on his post:

And where to put this? I am using Xcode 4.3.2.  From the Project Navigator, click on your project, then click on your target. Then click on the Build Phases. On the bottom right click on Add Build Phase, then Add Run Script. Drag the new section to right above Copy Bundle Resources. Then past the script above into it. And then check the box for Run scripts only when installing.

Seems to be working pretty well for me.  Ultimately I would like to get it where it combines a build number prefixed with the version number, but I did not have the time to figure that out.  If anybody else is doing it that way, leave me a comment.

Also the original script from Fredrik Olsson uses a separate variable, not the Xcode build variable.  If that sounds more appealing to you, check it out.

  • I also have the problem of it not incrementing until after the Info.plist is processed, therefore making your app on the device look one version behind. It’s been bugging me for days.

    • In saying that I have just found a workaround: in your AppDelegate increment the build number artificially to match Xcode:

      – (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

      {

      // Override point for customization after application launch.

      [self populateSettings]; // This populates the setting app.

      return YES;

      }

      -(void)populateSettings

      {

      NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

      NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@”CFBundleShortVersionString”];

      NSString *build = [[[NSBundle mainBundle] infoDictionary] objectForKey:@”CFBundleVersion”];

      // Convert build to int

      int buildInt = [build intValue] + 1;

      //Convert back to String

      build = [NSString stringWithFormat:@”%i”, buildInt];

      [defaults setObject:version forKey:@”AppVersion”];

      [defaults setObject:build forKey:@”BuildNumber”];

      [defaults synchronize];

      }

      • Thanks Stuart. I’m hoping to try it out in a week or two. I never thought about accessing and updating through the NSUserDefaults. I wouldn’t call it ideal, but functional. 🙂

        • Did you find another way to do this? It’s one of those small things that just keeps nagging at you.

          • Unfortunately not. I haven’t had a chance to mess with your solution yet either. I will take another look in a few weeks when I push out an update for my app.

            This kind of functionality should be built into Xcode. It’s really frustrating that we have to hack it.

          • Daniel Shelton

            Hi. I found a solution to the problem of being a version behind where you should be – I posted an answer on StackOverflow: http://stackoverflow.com/a/18550136/2078550

            The problem is that the plist file has already been processed by the time that your “Run Script” runs, so you need to also update the binary plist that has was produced by that processing.

            You should be able to get this to work by duplicating the plistbuddy command to set the version number, but use the filename “${CODESIGNING_FOLDER_PATH}/Info.plist”.

          • Thank you Daniel. Have you implemented this in one of your own projects yet?

          • Daniel Shelton

            Yes, using the Ruby code that I posted in the StackOverflow comment that I mentioned, but the same thing should work find with a shell script, I imagine.

            Thanks for your original post, it pointed me in the right direction.

Follow Us

Follow our activity on the web and get to know us.