RestKit: Post and Return Results
Posted on Tuesday, July 24th, 2012 under iOS, Rails
I’ve spent the last couple of weeks getting to know RestKit. We’re using it for some new internal projects including building the new Spots iOS App on it.
Something that I found interesting and with little documentation online is how to POST an object to a server over JSON but also to easily retain the results that are sent back. Here’s how:
Overview
In this example we’re going to be working with a User object which has two characteristics.
- ID
- Device ID
Nothing crazy, all nice and simple. We’re going to check if the Device ID already exists within the users table. If not, we’ll create it whilst retaining the newly created ID.
User Object
User.h
#import Foundation/Foundation.h @interface User : NSObject @property (nonatomic, retain) NSNumber* identifier; @property (nonatomic, retain) NSString *device_id; @end
User.m
#import "User.h" @implementation User @synthesize device_id, identifier; @end
Posting and Returning
I’ve already created an NSDictionary which contains all live User IDs and Device IDs call userList. I then check against the userList dictionary to see if the Device Id exists. If it doesn’t, create it and assign the newly created ID to currentUser.identifier:
if ([[usersList allKeys] containsObject:device_id) {
NSLog(@"User exists");
currentUser.identifier = [usersList objectForKey: device_id];
currentUser.device_id = device_id;
} else {
NSLog(@"Nope, never seen it");
RKObjectRouter *router = [RKObjectManager sharedManager].router;
[router routeClass:[User class] toResourcePath:@"/users.json" forMethod:RKRequestMethodPOST];
RKObjectMapping *userMapping = [RKObjectMapping mappingForClass:[User class]];
[userMapping mapKeyPath:@"device_id" toAttribute:@"device_id"];
[userMapping mapKeyPath:@"id" toAttribute:@"identifier"];
[[RKObjectManager sharedManager].mappingProvider registerMapping:userMapping withRootKeyPath:@"user"];
currentUser.device_id = device_id;
[[RKObjectManager sharedManager] postObject:currentUser usingBlock:^(RKObjectLoader *loader){
loader.onDidLoadObject = ^(id obj) {
NSLog(@"UserID: %@", currentUser.identifier);
};
}];
}
Getting social with iOS and Twitter
Posted on Monday, January 9th, 2012 under iOS, kickit
The next step for kickit was some social abilities. It would be a great addition to the app if the user could post to their friends on twitter about their progress. Kicking the habit is hard enough. The addition of help from friends and family would be a great benefit.
The Learning Curve
I had never added twitter posting abilities to an iOS application before. This was all going to be new to me, all over again. I started by digging through some of Apple’s extensive developer documentation. I discovered that built in to the SDK is a wonderful little twitter package. Perfect.
There are draw backs to using the integrated twitter method in iOS5. Mainly, it’s not backwards compatible. Anyone using pre iOS5 would get an alert asking them to update before they could update their kick ass followers on twitter. This didn’t deter me. Onwards and upwards.
kickit 1.3 Release Notes
Posted on Sunday, January 8th, 2012 under iOS, kickit
Here’s a list of bug fixes and feature improvements coming in the next release of kickit for iPhone and iPod touch. We’re hoping to have them in the app store by the end of January.
If you notice any bug that is not described below, feel free to pop us an email
kickit gets some healthy updates
Posted on Tuesday, December 20th, 2011 under Apps, iOS
Since the launch of kickit, I’ve received some great feedback. Financially incentivizing someone to kick the habit is a great motivational method. The other main reason to kick the habit is your health restoration. The longer you remain free from smoking, the greater you body’s health improves.
A comprehensive list of design tools
Posted on Monday, December 5th, 2011 under Apps, Design, iOS, Mac
Design is a creative endeavor than can run smoother with the right tools. I’ve put together a list of some of the tools I love to use when designing. Hopefully they’ll bring you as much joy as they’ve given me when I stumbled upon them:
Learning the road of iOS development
Posted on Wednesday, November 23rd, 2011 under Design, iOS, Learning
It’s been an ambition of mine to create an iOS application. I’ve watched other iOS developers create really awesome apps and wanted to get a piece of the action. Mainly from a learning standpoint. The goal of the first app being, creating an application that’s well designed and simplistic. I’ve never developed an iOS app before, everything was new to me. Where to start?
