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.
}




Read More

Making a HTTP Get Request in iOS

Creating a HTTP Get Request and parsing a JSON response in iOS...


// The url to your server endpoint
NSString *url = @"http://www.allaboutladybugs.info/get_bugs";
  
// Now, because I want my server to track the device by it's device UID, I'm going to send that along as a parameter
UIDevice *device = [UIDevice currentDevice];
NSString *deviceId = [device uniqueIdentifier];
  
// Here we are appending the device_id as a parameter with the URL
url = [NSString stringWithFormat:@"%@?device_id=%@", url, deviceId];
  
// Make the request, returning a JSON response, just as a plain old string.
NSData *jsonDataString = [[NSString stringWithContentsOfURL:[NSURL URLWithString:url] encoding:NSUTF8StringEncoding error: nil] dataUsingEncoding:NSUTF8StringEncoding];
  
// Keep a reference to a nil error so we can pass it into a JSON parser and learn of any errors
NSError *error = nil;
  
// Now, using the JSON parser, we'll get a dictionary representing our parsed JSON data
NSDictionary *results = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:&error];
  
// Now that we have an NSDictionary object containing the reconstituted results, we can obtain the objects we want from the dictionary using a key path like so...
  
NSArray *myResults = [results valueForKeyPath:@"bugs.ladybug"];
  
  // But, what the heck does this valueForKeyPath business mean in the context of a NSDictionary?
  //
  // Well, it means that if our json response looked like this:
  //
  //  { "bugs" =>
  //    [{ "ladybug" =>
  //       { "name": "Lola",
  //          "age_in_days": 23 }
  //     }]
  //  }
  //... then it would return, from the dictionary, an array of size 1 containing 1 element: an object
  // representing the ladybug 'Lola'
  // But how do we access this value?
  //
  // ... with objectForKey... like this...
  
// Just get the first element from the array
NSDictionary *bug = (NSDictionary *)[myResults objectAtIndex:0];
  
// And get its name
NSString *bugName = [bug objectForKey:@"name"];
  



Read More

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