Skip to content

Instantly share code, notes, and snippets.

@spilliams
Last active December 14, 2015 17:09
Show Gist options
  • Save spilliams/5120426 to your computer and use it in GitHub Desktop.
Save spilliams/5120426 to your computer and use it in GitHub Desktop.
steps for bootstrapping an iOS project

#Bootstrapping An iOS App

Note: If you're bootstrapping a static lib project, read this gist instead.

##Project Setup

  1. New xcode project (including save to directory).

  2. Set devices and iOS target

  3. decide if you're going to use Autolayout (set in the Storyboard > File inspector > Interface Builder Document section)

  4. Create Constants.h, and #import "Constants.h" in the PCH

  5. Update PCH to get DLog():

     // DLog is almost a drop-in replacement for NSLog
     // http://iphoneincubator.com/blog/debugging/the-evolution-of-a-replacement-for-nslog
     // DLog();
     // DLog(@"here");
     // DLog(@"value: %d", x);
     // Unfortunately this doesn't work DLog(aStringVariable); you have to do this instead DLog(@"%@", aStringVariable);
     #ifdef DEBUG
     #    define DLog(fmt, ...) NSLog((@"%s [line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
     #else
     #    define DLog(...)
     #endif
     
     // ALog always displays output regardless of the DEBUG setting
     #define ALog(fmt, ...) NSLog((@"%s [line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
    

##Manage Dependencies with CocoaPods

CocoaPods are almost certainly going to come in useful.

  1. Install: gem install cocoapods && pod setup
  2. Create Podfile with eg platform :ios, '6.0'
  3. pod install

##Generate a Docset with appledoc

Documentation is required!

  1. Install (see their site--IIRC it involves downloading an Xcode project and building to your Mac)

  2. Create a new Command Line Tool target called "Documentation".

  3. Add a build phase for "Run Script" with something similar to the following:

     # example script to run during an appledoc build target
     # replace instances of PROJECT with your project's name, and COMPANY with your company's name
     
     /usr/local/bin/appledoc \
     --project-name PROJECT \
     --project-company COMPANY \
     --company-id com.COMPANY \
     --logformat xcode \
     --keep-undocumented-objects \
     --keep-undocumented-members \
     --keep-intermediate-files \
     --no-repeat-first-par \
     --no-warn-invalid-crossref \
     --ignore .m \
     --ignore Pods \
     --ignore PROJECT/CoreData \
     --ignore Documentation \
     --ignore AppDelegate.h \
     --index-desc "${PROJECT_DIR}/README.md" \
     --output "${PROJECT_DIR}/Documentation" \
     "${PROJECT_DIR}"
    

##CoreData Stack!

CoreData isn't always necessary.

###CoreData

  1. add the CoreData framework to the Build Phases ("Link Binary With Libraries")
  2. #import <CoreData/CoreData.h> in the PCH (after Foundation is imported)
  3. Generate a .xcdatamodeld file
  4. Add entities, attributes and relations

From here you could copy down some boilerplate code and call it good. But to avoid the inevitable suicides there's a little more setup (you'll thank me later).

###MagicalRecord

MagicalRecord obscures a lot of the boilerplate necessary for CoreData. It also makes fetching, storing, deleting and updating records a lot simpler.

  1. Add pod "MagicalRecord" to Podfile and pod install

  2. #import "CoreData+MagicalRecord.h" in the PCH (after CoreData is imported)

  3. If you did generate CoreData boilerplate during project setup you'll want to delete it (@propertys and method declarations in AppDelegate.h, and @synthesizes and method implementations in AppDelegate.m

  4. Define static NSString* const kXXPersistentStoreName in Constants.h

  5. AppDelegate.m's -application:didFinishLaunchingWithOptions: gets a new line [MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:kXXPersistentStoreName];

  6. AppDelegate.m's -applicationWillTerminate: gets a new line [MagicalRecord cleanUp];

  7. Add a block to your Podfile that precludes MagicalRecord from spewing all over your logs

     post_install do |installer|
         installer.project.targets.each do |target|
             target.build_configurations.each do |config|
                 settings = config.build_settings['GCC_PREPROCESSOR_DEFINITIONS']
                 settings = settings || [ '$(inherited)' ]
                 settings << 'MR_ENABLE_ACTIVE_RECORD_LOGGING=0'
                 config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] = settings
             end
         end
     end
    

###Mogenerator

If you're using CoreData, you should use Mogenerator to generate your model files.

  1. brew update && brew [install|upgrade] mogenerator

  2. Add a build target "Mogenerator" with a script:

     cd PROJECT
     /usr/local/bin/mogenerator --template-var arc=true -m Model.xcdatamodeld/Model.xcdatamodel/ -M CoreData/Machine/ -H CoreData/Human/
    
  3. Build Mogenerator to generate CoreData models. Drag them from Finder into the project.

###Network Client

A network client is necessary if your app is going to be getting its data from the Internet.

You could write your own network client, or do something like NSManagedObject+Helpers, using AFNetworking.

Or you could use RESTKit. It's available as a pod. I'm not sure what's involved in using it, but they do have some nice example apps (in the download from their site). It seems like it doesn't play nicely with MR though...

##TestFlight

TestFlight is helpful for beta-testing. If you aren't going to do any closed tests before App Store release, move along.

TODO

##Version Control

Um, required.

  1. Create git repo
  2. git init && git remote add origin ___ && git pull
  3. Update README (see 2_readme.md attached)
  4. Commit & push!

##Dependencies

Dependencies are managed with Cocoapods. See their documentation for installation instructions.

To install dependencies, run

pod install

And make sure to use the .xcworkspace file, not the .xcodeproj file.

###Pods

  • MagicalRecord: copyright (c) 2010 Magical Panda Software, LLC under the MIT License.

##Documentation

Documentation is built with appledoc.

It can be viewed as html or as a docset in Xcode (only if you've built the Documentation target though). Xcode's docsets show up in a few places:

  1. The Documentation tab of the Organizer (command-shift-2, or alt-hover-doubleclick)
  2. The Quick Help inspector (command-alt-2)
  3. tooltips over code (alt-hover-click)

If you'd like to build the documentation, you will have to install appledoc on your particular system. See their documentation for how to do this. If you've built it before for a different project there's no need to build it again. Just build the Documentation target.

##Data Stack

Data is managed by CoreData with help from MagicalRecord and Mogenerator. If you'd like to change the schema, run the Mogenerator build target afterwards to automatically update the model files.

Mogenerator can be easily installed with Homebrew: brew update && brew install mogenerator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment