Created
August 30, 2012 04:31
-
-
Save ChrisRisner/3522334 to your computer and use it in GitHub Desktop.
iOS and Mobile Services 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma TodoDetailsViewControllerDelegate | |
- (void)todoDetailsViewController:(TodoDetailsViewController *)controller didFinishWithTodo:(NSString *)todoId andTodoText:(NSString *)todoText { | |
[self loadTodos]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma NSUrlConnectionDelegate Methods | |
-(void)connection:(NSConnection*)conn didReceiveResponse:(NSURLResponse *)response { | |
if (receivedData == NULL) { | |
receivedData = [[NSMutableData alloc] init]; | |
} | |
[receivedData setLength:0]; | |
httpResponse = (NSHTTPURLResponse*)response; | |
NSLog(@"Code: %i", [httpResponse statusCode]); | |
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 { | |
NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]); | |
if ([httpResponse statusCode]== 200 || [httpResponse statusCode] == 201) { | |
[self.navigationController popViewControllerAnimated:YES]; | |
[self.delegate todoDetailsViewController:self didFinishTodo:nil andTodoText:nil]; | |
} else { | |
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure" | |
message:@"Failed to mark item completed." | |
delegate:self | |
cancelButtonTitle:@"OK" | |
otherButtonTitles:nil]; | |
[alert show]; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@synthesize addingNewTodo; | |
@synthesize todoText; | |
@synthesize delegate; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (IBAction)tapMarkTodoComplete:(id)sender { | |
NSURL *urlUpdate = [[ NSURL alloc ] initWithString:[kUpdateUrl stringByAppendingFormat:@"%@",todoId] ]; | |
NSMutableURLRequest *theRequest=[NSMutableURLRequest | |
requestWithURL:urlUpdate | |
cachePolicy:NSURLRequestUseProtocolCachePolicy | |
timeoutInterval:60.0]; | |
[theRequest setHTTPMethod:@"PATCH"]; | |
[theRequest addValue:@"application/json" forHTTPHeaderField:@"ACCEPT"]; | |
[theRequest addValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; | |
[theRequest addValue:kMobileServiceAppId forHTTPHeaderField:@"X-ZUMO-APPLICATION"]; | |
//build an info object and convert to json | |
NSDictionary* jsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys: | |
@"true", @"complete", | |
todoId, @"id", | |
todoText, @"text", | |
nil]; | |
//convert JSON object to data | |
NSError *error; | |
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary | |
options:NSJSONWritingPrettyPrinted error:&error]; | |
[theRequest setHTTPBody:jsonData]; | |
// create the connection with the request and start loading the data | |
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; | |
if (theConnection) { | |
// Create the NSMutableData to hold the received data. | |
// receivedData is an instance variable declared elsewhere. | |
receivedData = [NSMutableData data]; | |
} else { | |
// We should inform the user that the connection failed. | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (IBAction)tapSaveTodo:(id)sender { | |
NSMutableURLRequest *theRequest=[NSMutableURLRequest | |
requestWithURL: | |
[NSURL URLWithString:kAddUrl] | |
cachePolicy:NSURLRequestUseProtocolCachePolicy | |
timeoutInterval:60.0]; | |
[theRequest setHTTPMethod:@"POST"]; | |
[theRequest addValue:@"application/json" forHTTPHeaderField:@"ACCEPT"]; | |
[theRequest addValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; | |
[theRequest addValue:kMobileServiceAppId forHTTPHeaderField:@"X-ZUMO-APPLICATION"]; | |
//build an info object and convert to json | |
NSDictionary* jsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys: | |
@"false", @"complete", | |
txtTodoText.text, @"text", | |
nil]; | |
//convert JSON object to data | |
NSError *error; | |
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary | |
options:NSJSONWritingPrettyPrinted error:&error]; | |
[theRequest setHTTPBody:jsonData]; | |
// create the connection with the request and start loading the data | |
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; | |
if (theConnection) { | |
// Create the NSMutableData to hold the received data. | |
// receivedData is an instance variable declared elsewhere. | |
receivedData = [NSMutableData data]; | |
} else { | |
// We should inform the user that the connection failed. | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
// Do any additional setup after loading the view. | |
if (addingNewTodo == NO) { | |
//Viewing an existing todo | |
viewCreateTodo.hidden = YES; | |
lblTodoText.text = todoText; | |
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; | |
for (NSDictionary *item in appDelegate.todos) { | |
if ([todoText isEqualToString:[item objectForKey:@"text"]]){ | |
NSLog(@"Item: %@", item); | |
todoId = (NSNumber*) [item objectForKey:@"id"]; | |
break; | |
} | |
} | |
} else { | |
//Adding a new todo | |
viewDetailsTodo.hidden = YES; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, | |
NSURLConnectionDelegate, TodoDetailsViewControllerDelegate> { | |
@private | |
NSMutableData* receivedData; | |
} | |
@property (weak, nonatomic) IBOutlet UITableView *tableView; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { | |
if ([segue.identifier isEqualToString:@"viewExisitngTodo"]) | |
{ | |
TodoDetailsViewController *todoDetailsViewController = segue.destinationViewController; | |
UITableViewCell *cell = (UITableViewCell *)sender; | |
todoDetailsViewController.todoText = cell.textLabel.text; | |
todoDetailsViewController.delegate = self; | |
todoDetailsViewController.addingNewTodo = NO; | |
} else if ([segue.identifier isEqualToString:@"addNewTodo"]) { | |
TodoDetailsViewController *todoDetailsViewController = segue.destinationViewController; | |
UITableViewCell *cell = (UITableViewCell *)sender; | |
todoDetailsViewController.delegate = self; | |
todoDetailsViewController.addingNewTodo = YES; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment