Sample Application
The ObjectiveResource XCode project includes a sample application. If you would like get a quick feel for the framework open the project in XCode and have a look around.
To see the framework in action, the first step is to run the included Rails server in “sample_rails_app” (./script/server). Next, in XCode, change the default target from “Unit Tests” to “objective_resource” and then “Build and Run.” The project should compile and launch the simulator.
Have a look at the Rails log and the XCode console (when in debug mode) to see how ObjectiveResource interacts with Rails over the wire.
Usage
Once you’ve downloaded and installed ObjectiveResouce you can begin integrating your iPhone and Rails apps.
ObjectiveResource adds methods to NSObject using a the NSObject+ObjectiveResource category, so any Objective-C class can be treated as a remote resource.
The name of the class and the property names should match those of a remote Rails resource. The one
exception is the id property. Since id is a reserved word in Objective-C, ObjectiveResource maps
that Rails property to ClassName@Id. (So for @dog it would be the dogId property). The code below illustrates a dog class that would map to a Rails dog resource.
1 2 3 4 5 6 7 |
@interface Dog : NSObject { NSString *name; NSString *dogId; } @property (nonatomic , retain) NSString *name; @property (nonatomic , retain) NSString *dogId; //this maps to the Rails "id" property @end |
The ObjectiveResourceConfig class is used for system wide configuration
1 2 3 4 5 6 7 8 9 10 |
//Set the address of the rails site. The trailing slash is required [ObjectiveResourceConfig setSite:@"http://localhost:3000/"]; //Set the username and password to be used for the remote site [ObjectiveResourceConfig setUser:@"remoteResourceUserName"]; [ObjectiveResourceConfig setPassword:@"remoteResourcePassword"]; //Set ObjectiveResource to use either XML or JSON [ObjectiveResourceConfig setResponseType:XmlResponse];//the default [ObjectiveResourceConfig setResponseType:JSONResponse]; |
All CRUD operations are supported
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
//Create Dog *dog = [[[Dog alloc] init] autorelease]; dog.name = @"Fido"; [dog saveRemote]; //Read NSArray *dogs = [Dog findAllRemote]; dog = [Dog findRemote:dog.dogId]; //Update dog.name = @"Fido Jones"; [dog updateRemote]; //Delete [dog destroyRemote]; |
Support for nested resources can be added. For example, to add support for finding all dogs owned by a person, the following method could be added to the Dog class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
+ (NSArray *)findAllForPersonWithId:(NSString *)personId { // use ObjectiveResource URL generation methods to construct the nested path of // http://dogshopserver/persons/:person_id/dogs.xml NSString *dogPersonPath = [NSString stringWithFormat:@"%@%@/%@/%@%@", [self getRemoteSite], [Person getRemoteCollectionName], personId, [self getRemoteCollectionName], [self getRemoteProtocolExtension]]; Response *res = [Connection get:dogPersonPath withUser:[[self class] getUser] andPassword:[[self class] getPassword]]; return [self allFromXMLData:res.body]; } |
Another common requirement is to support a custom, non CRUD action. Lets say we want to
add a pet action to our dog resource. This action would reside at http://dogshopserver/dogs/1/pet.xml.
Lets take a look at how we can add a “pet” method to our Dog class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
- (void) pet { //construct the path to the custom "pet" action NSString *petPath = [NSString stringWithFormat:@"%@%@/%@/pet%@", [Dog getRemoteSite], [Dog getRemoteCollectionName], dogId, [Dog getRemoteProtocolExtension]]; //send the response Response *res = [Connection get:petPath withUser:[[self class] getUser] andPassword:[[self class] getPassword]]; //update our properties with those returned from the server [self setProperties:[[Dog fromXMLData:res.body] properties]; } |
ObjectiveResource will log all HTTP requests and responses if debug logging is enabled. To enable debug logging in XCode for the Debug configuration follow these steps
- In the Project menu, select “Edit Project Settings”
- Select the “Build” tab
- Change the Configuration drop down list to “Debug”
- Set the GCC_PREPROCESSOR_DEFINITIONS setting to “DEBUG” (you may need to add this as a new setting)
Other Resources
- Introduction to ObjectiveResource screencast
- The iPhoneOnRails Community
- ObjectiveResource Source
- Pragmatic Programmers iPhone on Rails Screencast Series (coming soon)
