Skip to content

Instantly share code, notes, and snippets.

@mk
Created December 20, 2011 12:17
Show Gist options
  • Save mk/1501382 to your computer and use it in GitHub Desktop.
Save mk/1501382 to your computer and use it in GitHub Desktop.
HOWTO: Git generate and read revision for XCode/ObjC

git revision for Xcode

  1. Install custom build phase In Xcode go to the project settings: Add build phase => Add run script and paste

     `which git` show  --abbrev-commit --pretty=oneline | head -1 | cut -d " " -f1 > revision
    

    This should go before Copy resource bundle

  2. Add revision to Copy resource bundles

  3. Load revision in Application by adding a Category on Application

     - (NSString*)revision {
       static NSString* revisionNumber = nil; 
       if (revisionNumber == nil) {
         NSString* revisionFile = [[NSBundle mainBundle] pathForResource:@"revision" ofType:nil];
         if (revisionFile) {
           NSData* data = [NSData dataWithContentsOfFile:revisionFile];
           NSString* fileContent = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
           revisionNumber = [fileContent stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
         }
         if (revisionNumber == nil) {
           revisionNumber = @"0000000";
         }
       }
       return revisionNumber; 
     }
    

    Can be added as a property as well.

  4. Load the revision from your code

     [[UIApplication sharedApplication] revision]
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment