Skip to content

Instantly share code, notes, and snippets.

@jefflinwood
Created March 17, 2012 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jefflinwood/2061788 to your computer and use it in GitHub Desktop.
Save jefflinwood/2061788 to your computer and use it in GitHub Desktop.
Drupal iOS SDK with blocks instead of delegates - example DIOSView class, with implementation in DrupalMapApp
//
// DIOSView.h
// DrupalMapApp
//
// Created by Jeffrey Linwood on 3/17/12.
// Copyright (c) 2012 Jeff Linwood. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "AFHTTPRequestOperation.h"
@interface DIOSView : NSObject
/**
Returns the contents of the specified Drupal view. Implementation takes two blocks, one for success, and one for failure. Either parameter may be nil.
@param success The block to be executed on the completion of a successful request. This block has no return value and takes two arguments: the receiver operation and the object constructed from the response data of the request.
@param failure The block to be executed on the completion of an unsuccessful request. This block has no return value and takes two arguments: the receiver operation and the error that occured during the request.
*/
- (void)getView:(NSString *)viewName success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure;
@end
//
// DIOSView.m
// DrupalMapApp
//
// Created by Jeffrey Linwood on 3/17/12.
// Copyright (c) 2012 Jeff Linwood. All rights reserved.
//
#import "DIOSView.h"
#import "DIOSSession.h"
@implementation DIOSView
/**
Returns the contents of the specified Drupal view. Implementation takes two blocks, one for success, and one for failure. Either parameter may be nil.
@param success The block to be executed on the completion of a successful request. This block has no return value and takes two arguments: the receiver operation and the object constructed from the response data of the request.
@param failure The block to be executed on the completion of an unsuccessful request. This block has no return value and takes two arguments: the receiver operation and the error that occured during the request.
*/
- (void)getView:(NSString *)viewName success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
[[DIOSSession sharedSession] getPath:[NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseView, viewName] parameters:nil success:success failure:failure];
}
@end
- (void)viewDidAppear:(BOOL)animated{
if ([[[AppData sharedInstance] businesses] count] != 0) {
//we've already loaded data once for this app, so just display it on the map
[self loadMapData];
} else {
[SVProgressHUD showWithStatus:@"Retrieving Data"];
DIOSView *diosView = [[DIOSView alloc] init];
[diosView getView:@"businesses.json" success:^(AFHTTPRequestOperation *operation, id responseObject) {
//store results from the view into the application wide data
[[AppData sharedInstance] storeResults:responseObject];
[self loadMapData];
[SVProgressHUD dismissWithSuccess:@"Downloaded"];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[SVProgressHUD dismissWithError:@"Unable to download data" afterDelay:5];
}];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment