Skip to content

Instantly share code, notes, and snippets.

@ChrisRisner
Created August 29, 2012 21:36
Show Gist options
  • Save ChrisRisner/3519278 to your computer and use it in GitHub Desktop.
Save ChrisRisner/3519278 to your computer and use it in GitHub Desktop.
Mobile Services and iOS
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) NSArray *todos;
@end
@implementation AppDelegate
@synthesize todos;
@interface Constants : NSObject
extern NSString *kGetAllUrl;
extern NSString *kAddUrl;
extern NSString *kUpdateUrl;
extern NSString *kMobileServiceAppId;
@end
@implementation Constants
NSString *kGetAllUrl = @"https://yoursubdomain.azure-mobile.net/tables/TodoItem?$filter=(complete%20eq%20false)";
NSString *kAddUrl = @"https://yoursubdomain.azure-mobile.net/tables/TodoItem";
NSString *kUpdateUrl = @"https://yoursubdomain.azure-mobile.net/tables/TodoItem/";
NSString *kMobileServiceAppId = @"yourappkey";
@end
-(void) loadTodos {
NSMutableURLRequest *theRequest=[NSMutableURLRequest
requestWithURL:
[NSURL URLWithString: kGetAllUrl]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[theRequest setHTTPMethod:@"GET"];
[theRequest addValue:@"application/json" forHTTPHeaderField:@"ACCEPT"];
[theRequest addValue:kMobileServiceAppId forHTTPHeaderField:@"X-ZUMO-APPLICATION"];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
receivedData = [NSMutableData data];
} else {
// We should inform the user that the connection failed.
}
}
#pragma NSUrlConnectionDelegate Methods
-(void)connection:(NSConnection*)conn didReceiveResponse:(NSURLResponse *)response {
if (receivedData == NULL) {
receivedData = [[NSMutableData alloc] init];
}
[receivedData setLength:0];
NSLog(@"didReceiveResponse: responseData length:(%d)", receivedData.length);
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Append the new data to receivedData.
// receivedData is an instance variable declared elsewhere.
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error {
// inform the user
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSError* error;
NSArray* json = [NSJSONSerialization
JSONObjectWithData:receivedData
options:kNilOptions
error:&error];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.todos = json;
[self.tableView reloadData];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
return [appDelegate.todos count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSLog( @"Indexpath %i", [ indexPath row ] );
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
cell.textLabel.text = [[appDelegate.todos objectAtIndex:[indexPath row]] objectForKey:@"text"];
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
@class TodoDetailsViewController;
@protocol TodoDetailsViewControllerDelegate <NSObject>
- (void)todoDetailsViewController:(TodoDetailsViewController *)controller didFinishTodo:(NSString *)todoId andTodoText:(NSString *)todoText;
@end
@interface TodoDetailsViewController : UIViewController<NSURLConnectionDelegate> {
@private
NSNumber* todoId;
NSMutableData* receivedData;
NSHTTPURLResponse* httpResponse;
}
@property (weak, nonatomic) IBOutlet UIView *viewCreateTodo;
@property (weak, nonatomic) IBOutlet UIView *viewDetailsTodo;
@property (weak, nonatomic) IBOutlet UITextField *txtTodoText;
@property (weak, nonatomic) IBOutlet UILabel *lblTodoText;
@property (nonatomic, weak) id <TodoDetailsViewControllerDelegate> delegate;
@property (nonatomic, weak) NSString *todoText;
@property BOOL addingNewTodo;
- (IBAction)tapSaveTodo:(id)sender;
- (IBAction)tapMarkTodoComplete:(id)sender;
@end
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self loadTodos];
}
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource,
NSURLConnectionDelegate> {
@private
NSMutableData* receivedData;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment