-
Notifications
You must be signed in to change notification settings - Fork 298
Guide:Adding Couchbase Lite To Your App
Jens Alfke edited this page Feb 2, 2013
·
10 revisions
Assuming you're ready to start using Couchbase Lite, here's how to get started.
- Obtain the
CouchbaseLite.framework
: - Download the latest release.
- Move the downloaded
Couchbase Lite
folder wherever you'd like to keep it. - Open the subfolder for your platform — iOS or Mac OS.
- Drag
CouchbaseLite.framework
from that subfolder into the file list of your app's project window. (I recommend adding it to the "Frameworks" group, but it's not necessary.) - In the window that appears, make sure the checkbox next to your app's target is checked.
- For an iOS target:
- Go to the "Build Settings" tab of your app target's settings.
- Find the "Other Linker Flags" row in the "Linker" section, and add the flag
-ObjC
. (Note the capitalization.) - Go to the "Build Phases" tab of the app target's settings.
- In the "Link Binary with Libraries" section, click the plus button and add
CFNetwork.framework
,Security.framework
,SystemConfiguration.framework
,libsqlite3.dylib
andlibz.dylib
. - Or for a Mac OS target, you'll need to make sure the framework is copied into your app's bundle:
- To to the "Build Settings" tab of your app target's settings.
- In the "Linker" section, edit "Runpath Search Paths" and add the path "
@loader_path/../Frameworks
". - Now switch to to the "Build Phases" tab.
- Press the "Add Build Phase" button at the bottom of the window and create a new "Copy Files" phase.
- Set the phase's destination to "Frameworks".
- Press the "+" button below the phases' file list and choose CouchbaseLite.framework from the list that pops up.
- Build your app to make sure there are no errors.
(For more details, and for a description of how to check out and build Couchbase Lite from source, consult the wiki page.)
Now you're ready to write code. The first step is to initialize Couchbase Lite, probably in your application delegate's applicationDidFinishLaunching
method. First add the necessary import directive:
#import <CouchCocoa/CouchCocoa.h>
Then add the following to the applicationDidFinishLaunching
method:
CBLManagerServer* server = [CBLManagerServer sharedInstance];
if (server.error) [self failed: server.error];
self.database = [server databaseNamed: @"my-database"]; // db name must be lowercase!
NSError* error;
if (![self.database ensureCreated: &error]) [self failed: error];
- The call to
[self failed:]
is a placeholder for your actual error handling/reporting code -- if Couchbase Lite is crucial to your app, you may have no option but to put up a fatal-error alert and then exit. - This code creates a database named
my-database
. The name's not very important, but you'll probably want to personalize it anyway. Be aware that Couchbase Lite does not allow uppercase letters in database names: legal characters are lowercase letters, digits, and any of_$()+-/
. - The
CBLDatabase
object representing the database is stored into adatabase
property of the app delegate. You'll need to declare and synthesize this property in the usual way.
(TBD)
NEXT: Working With Documents