iAmber - Part One, Adding CouchDB to XCODE 4.2 IOS 5 Project
What is CouchDB and how do i get it.
CouchDB is a noSQL document orientated database. It stores data in a document JSON, like format, and is accessible through a RESTful JSON API. adhoc and scheme-free.
CouchDB Couchbase is a server implementation and framework that extends the Apache CouchDB. When we are talking about CouchDB, we mean Couchbase. When implementing couchdb into your iOS5 projects, you'll need to add the couchbase IOS framework to your project.
Download from the Couchbase IOS site or the GIT repo.
> git clone https://github.com/couchbaselabs/iOS-Couchbase.git couchbase
Drag both the couchbase and cococouch to your frameworks group folder. making sure that you app is selected under targets.
Select your project and move to the build phase.
add the libraries under Link Binary With Libraries.
add the copy Couchdb script under run scripts.
rsync -a "${SRCROOT}/Couchbase.framework/CouchbaseResources" "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}"
check the SRCROOT/ path to Couchbase.framework to verify your path to the framework.
2. Â Wire the Appdelegate and start CouchDB in your project.
#import <Couchbase/CouchbaseMobile.h>
@class CouchDatabase, RootViewController;
 @interface AppDelegate : UIResponder <UIApplicationDelegate,CouchbaseDelegate>{
  CouchDatabase *database;  }
@property(nonatomic, retain)CouchDatabase *database;
Set the import and define database variables.
#import <CouchCocoa/CouchCocoa.h>
// The name of the database the app will use.
#define kDatabaseName @"amber"
// Set this to 1 to install a pre-built database from a ".couch" resource file on first run.
#define INSTALL_CANNED_DATABASE 0
Now in the application didFinishLaunchingWithOptions
  [self performSelector: @selector(connectToServer:)
        withObject: [NSURL URLWithString: USE_REMOTE_SERVER]
        afterDelay: 0.0];
  CouchbaseMobile* couchbase = [[CouchbaseMobile alloc] init];
  couchbase.delegate = self;
#if INSTALL_CANNED_DATABASE
  NSString* dbPath = [[NSBundle mainBundle] pathForResource: kDatabaseName ofType: @"couch"];
  NSAssert(dbPath, @"Couldn't find "kDatabaseName".couch");
  [gCouchbaseMobile installDefaultDatabase: dbPath];
  if (![couchbase start]) {
    [self showAlert: @"Couldn't start Couchbase."
         error: couchbase.errorÂ
         fatal: YES];
Now add the connectToServer method and add the couchbaseMobile delegates
- (void)connectToServer:(NSURL*)serverURL {
  NSLog(@"iAmber:amber: couchbaseMobile:didStart: <%@>", serverURL);
    // This is the first time the server has started:
    CouchServer *server = [[CouchServer alloc] initWithURL: serverURL];
    self.database = [server databaseNamed: kDatabaseName];
    //[server release];
#if !INSTALL_CANNED_DATABASE
    // Create the database on the first run of the app.
    if (![[self.database GET] wait])
      [[self.database create] wait];
  database.tracksChanges = YES;
  database.tracksActiveOperations = YES;
-(void)couchbaseMobile:(CouchbaseMobile*)couchbase didStart:(NSURL*)serverURL {
  NSLog(@"Couchbase is Ready, go! %@", serverURL);
  [self connectToServer:serverURL];
-(void)couchbaseMobile:(CouchbaseMobile*)couchbase failedToStart:(NSError*)error {
  NSAssert(NO, @"Couchbase failed to initialize: %@", error);
When you run your project in the simulator you'll get couchbase server started and a log entries to verify the database creation.