Ready to Level-Up?

This just in! If your rusty iOS skills could use some refining, or if you're just starting out and you want to level-up fast, consider signing up for our upcoming iOS Screencasts

New from iOS Rocket Surgery Swift Video Tutorials: The rapidly evolving Apple-iOS ecosystem has just been thrown into tumult with the recent introduction of the Swift Programming Language. Are you ready to finally try iOS development? Are you needing to switch from Objective-C to Swift? Sign up today for our free video courses!
(By the dear folks at Swift Tutorial Videos, a sister company)

Friday, September 28, 2012

Creating a Database in iOS

No fluff, here we go, setting up a database in iOS, XCode, Objective-c ...

// We're going to create a database in the User's default document directory
NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:(NSDocumentDirectory) inDomains:(NSUserDomainMask)] lastObject];

// We're going to name the database file MyArbitraryDatabaseName
url = [url URLByAppendingPathComponent:@"MyArbitraryDatabaseName"];

// Create a UIManagedDocument object that we will use to create the database
UIManagedDocument *database = [[UIManagedDocument alloc] initWithFileURL:url];

// Now, if the database doesn't already exists in that file, we'll create it.
if(![[NSFileManager defaultManager] fileExistsAtPath:[database.fileURL path]]){
  [database saveToURL:database.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success){
    // Do whatever you want after the database is created
  }];
} else if(database.documentState == UIDocumentStateClosed){
  [database openWithCompletionHandler:^(BOOL success){
    // Do whatever you want after the database is opened
  }];
} else if(database.documentState == UIDocumentStateClosed){
  // The database is already open, you've got a reference to it (database), so you're ready to go.
}






About Me

Easy NSDateFormatter Tool

Save yourself some time in formatting your NSDates to NSStrings, and use the Blind NSDate app, which you can download from iTunes. There's also a website where you can format your NSDates: http://www.blindnsdate.com

Popular Posts

Designed By Seo Blogger Templates