Skip to content

Instantly share code, notes, and snippets.

@chrisallick
Created March 5, 2013 22:14
Show Gist options
  • Save chrisallick/5094823 to your computer and use it in GitHub Desktop.
Save chrisallick/5094823 to your computer and use it in GitHub Desktop.
Retrieve JSON in iOS 6. It's that simple.
Sinatra:
get '/messages' do
content_type :json
messages = []
all = $redis.lrange("messages", 0, $redis.llen("messages"))
all.each do |message|
messages.push( message )
end
{ :result => "success", :messages => messages }.to_json
end
Objective-C:
-(void)getMessages {
NSURL *url = [NSURL URLWithString:@"http://localhost:9292"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
[httpClient setParameterEncoding:AFJSONParameterEncoding];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET"
path:@"messages"
parameters:nil];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"messages: %@", [JSON valueForKeyPath:@"messages"]);
} failure:nil];
[operation start];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment