Skip to content

Instantly share code, notes, and snippets.

@neerajgoel82
Last active February 6, 2017 14:04
Show Gist options
  • Save neerajgoel82/744a0175d553070edab1 to your computer and use it in GitHub Desktop.
Save neerajgoel82/744a0175d553070edab1 to your computer and use it in GitHub Desktop.
This is the gist of using RestKit with iOS app
Venue.h
-------
@class Location;
@class Stats ;
@interface Venue : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) Location *location;
@property (nonatomic, strong) Stats *stats;
@end
Location.h
----------
#import <Foundation/Foundation.h>
@interface Location : NSObject
@property (nonatomic, strong) NSString *address;
@property (nonatomic, strong) NSString *city;
@property (nonatomic, strong) NSString *country;
@property (nonatomic, strong) NSString *crossStreet;
@property (nonatomic, strong) NSString *postalCode;
@property (nonatomic, strong) NSString *state;
@property (nonatomic, strong) NSNumber *distance;
@property (nonatomic, strong) NSNumber *lat;
@property (nonatomic, strong) NSNumber *lng;
@end
Stats.h
--------
@interface Stats : NSObject
@property (nonatomic, strong) NSNumber *checkins;
@property (nonatomic, strong) NSNumber *tips;
@property (nonatomic, strong) NSNumber *users;
@end
ViewController.m
----------------
#import <RestKit/RestKit.h>
#import "Venue.h"
#import "Location.h"
#import "Stats.h"
- (void)configureRestKit
{
// initialize AFNetworking HTTPClient
NSURL *baseURL = [NSURL URLWithString:@"https://api.foursquare.com"];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
// initialize RestKit
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
// setup object mappings
RKObjectMapping *venueMapping = [RKObjectMapping mappingForClass:[Venue class]];
[venueMapping addAttributeMappingsFromArray:@[@"name"]];
RKObjectMapping *statsMapping = [RKObjectMapping mappingForClass:[Stats class]];
[statsMapping addAttributeMappingsFromDictionary:@{@"checkinsCount": @"checkins", @"tipsCount": @"tips", @"usersCount": @"users"}];
[venueMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"stats" toKeyPath:@"stats" withMapping:statsMapping]];
// define location object mapping
RKObjectMapping *locationMapping = [RKObjectMapping mappingForClass:[Location class]];
[locationMapping addAttributeMappingsFromArray:@[@"address", @"city", @"country", @"crossStreet", @"postalCode", @"state", @"distance", @"lat", @"lng"]];
// define relationship mapping
[venueMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"location" toKeyPath:@"location" withMapping:locationMapping]];
// register mappings with the provider using a response descriptor
RKResponseDescriptor *responseDescriptor =
[RKResponseDescriptor responseDescriptorWithMapping:venueMapping
method:RKRequestMethodGET
pathPattern:@"/v2/venues/search"
keyPath:@"response.venues"
statusCodes:[NSIndexSet indexSetWithIndex:200]];
[objectManager addResponseDescriptor:responseDescriptor];
}
- (void)loadVenues
{
NSString *latLon = @"37.33,-122.03"; // approximate latLon of The Mothership (a.k.a Apple headquarters)
NSString *clientID = kCLIENTID;
NSString *clientSecret = kCLIENTSECRET;
NSDictionary *queryParams = @{@"ll" : latLon,
@"client_id" : clientID,
@"client_secret" : clientSecret,
@"categoryId" : @"4bf58dd8d48988d1e0931735",
@"v" : @"20140118"};
[[RKObjectManager sharedManager] getObjectsAtPath:@"/v2/venues/search"
parameters:queryParams
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
_venues = mappingResult.array;
NSLog(@"Venues: %@", _venues);
[self.tableView reloadData];
}
failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"What do you mean by 'there is no coffee?': %@", error);
}];
}
- (void)viewDidLoad
{
[super viewDidLoad];
if(self.venueRequested == NO){
[self configureRestKit];
[self loadVenues];
self.venueRequested =YES ;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment