#Bootstrapping An iOS App
Note: If you're bootstrapping a static lib project, read this gist instead.
##Project Setup
-
New xcode project (including save to directory).
-
Set devices and iOS target
-
decide if you're going to use Autolayout (set in the Storyboard > File inspector > Interface Builder Document section)
-
Create
Constants.h
, and#import "Constants.h"
in the PCH -
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.
- Install:
gem install cocoapods && pod setup
- Create Podfile with eg
platform :ios, '6.0'
pod install
##Generate a Docset with appledoc
Documentation is required!
-
Install (see their site--IIRC it involves downloading an Xcode project and building to your Mac)
-
Create a new Command Line Tool target called "Documentation".
-
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
- add the CoreData framework to the Build Phases ("Link Binary With Libraries")
#import <CoreData/CoreData.h>
in the PCH (after Foundation is imported)- Generate a
.xcdatamodeld
file - 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 obscures a lot of the boilerplate necessary for CoreData. It also makes fetching, storing, deleting and updating records a lot simpler.
-
Add
pod "MagicalRecord"
to Podfile andpod install
-
#import "CoreData+MagicalRecord.h"
in the PCH (after CoreData is imported) -
If you did generate CoreData boilerplate during project setup you'll want to delete it (
@property
s and method declarations inAppDelegate.h
, and@synthesize
s and method implementations inAppDelegate.m
-
Define
static NSString* const kXXPersistentStoreName
inConstants.h
-
AppDelegate.m
's-application:didFinishLaunchingWithOptions:
gets a new line[MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:kXXPersistentStoreName];
-
AppDelegate.m
's-applicationWillTerminate:
gets a new line[MagicalRecord cleanUp];
-
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.
-
brew update && brew [install|upgrade] mogenerator
-
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/
-
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 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.
- Create git repo
git init && git remote add origin ___ && git pull
- Update README (see
2_readme.md
attached) - Commit & push!