Skip to content

Instantly share code, notes, and snippets.

@timstephenson
Last active December 15, 2015 05:19
Show Gist options
  • Save timstephenson/5207709 to your computer and use it in GitHub Desktop.
Save timstephenson/5207709 to your computer and use it in GitHub Desktop.
Hi Geoffrey Here's a simple category on NSDictionary for making JSON requests and converting data.
#import <Foundation/Foundation.h>
@interface NSDictionary (JSONCategory)
+ (NSDictionary*)dictionaryWithContentsOfJSONURLString: (NSString*)url;
- (NSData*)toJSON;
- (void)pushToURLString: (NSString *)urlString method:(NSString *)method delegate: (id)delegate;
@end
#import "NSDictionary+JSONCategory.h"
@implementation NSDictionary (JSONCategory)
+(NSDictionary*)dictionaryWithContentsOfJSONURLString: (NSString*)url {
NSData* data = [ NSData dataWithContentsOfURL:
[NSURL URLWithString: url] ];
__autoreleasing NSError* error = nil;
if (data == nil) return nil;
id result = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error];
if (error != nil) return nil;
return result;
}
-(NSData*)toJSON {
NSError* error = nil;
id result = [NSJSONSerialization dataWithJSONObject:self
options:kNilOptions
error:&error];
if (error != nil) return nil;
return result;
}
- (void)pushToURLString: (NSString *)urlString method:(NSString *)method delegate: (id)delegate {
//convert object to data
NSData *jsonData = [self toJSON];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL: [NSURL URLWithString: urlString]];
[request setHTTPMethod:method];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:jsonData];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:delegate];
[connection start];
}
@end
// Sample of how it is used to make a get request.
// This is from a game I am working on, so it is passing the lat lon coordinates and returning a list of objects
// that will be placed on the map.
NSString *url = [NSString stringWithFormat:@"%@/%@&radius=%d", kBaseURLString, [self appendLocationToPath:@"items.json"], radius];
dispatch_async(kBgQueue, ^{
NSDictionary *data = [NSDictionary dictionaryWithContentsOfJSONURLString:url];
[self performSelectorOnMainThread:@selector(loadItems:)
withObject:data waitUntilDone:YES];
});
// A sample of posting data back to the server.
// This passes the user data, and a list of items that the user has collected to the server.
NSDictionary *data = @{
@"items" : collectedItems,
@"user" :currentUser.toDictionary
};
NSString *url = [ NSString stringWithFormat:@"%@/items/collect.json", kBaseURLString ];
[ data pushToURLString: url method:@"PUT" delegate: self ];
// In this case I handle the response in the
// "connection:(NSURLConnection *)connection didReceiveData:(NSData *)data" delegate method
// because there are multiple requests with very near identical responses so it was
// easier to handle them all at once.
// Always interested in feedback if you have any ideas.
// Best
// Tim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment