Skip to content

Instantly share code, notes, and snippets.

@ChrisRisner
Created September 21, 2012 21:18
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 ChrisRisner/3763962 to your computer and use it in GitHub Desktop.
Save ChrisRisner/3763962 to your computer and use it in GitHub Desktop.
MobileServices and iOS 2
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo {
imageViewNewTodo.image = image;
[picker dismissModalViewControllerAnimated:YES];
}
- (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"];
NSData *data = nil;
NSString *imageData = nil;
if (imageViewNewTodo.image != nil) {
UIImage *image = imageViewNewTodo.image;
data = UIImagePNGRepresentation(image);
imageData = [data base64EncodedString];
}
//build an info object and convert to json
NSDictionary* jsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
@"false", @"complete",
txtTodoText.text, @"text",
imageData, @"coltest",
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.
}
}
- (IBAction)tapSelectImage:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
[self presentModalViewController:picker animated:YES];
}
@class TodoDetailsViewController;
@protocol TodoDetailsViewControllerDelegate <NSObject>
- (void)todoDetailsViewController:(TodoDetailsViewController *)controller didFinishWithTodo:(NSString *)todoId andTodoText:(NSString *)todoText;
@end
@interface TodoDetailsViewController : UIViewController<NSURLConnectionDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
@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 (weak, nonatomic) IBOutlet UIImageView *imageViewNewTodo;
@property (weak, nonatomic) IBOutlet UIImageView *imageViewExistingTodo;
@property (nonatomic, weak) NSString *todoText;
@property BOOL addingNewTodo;
- (IBAction)tapSaveTodo:(id)sender;
- (IBAction)tapMarkTodoComplete:(id)sender;
- (IBAction)tapSelectImage:(id)sender;
@end
- (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"]]){
todoId = (NSNumber*) [item objectForKey:@"id"];
NSString *stringData = (NSString *)[item objectForKey:@"coltest"];
if (stringData != (id)[NSNull null]) {
NSData *data = [NSData dataFromBase64String:stringData];
UIImage *image = [UIImage imageWithData:data];
imageViewExistingTodo.image = image;
}
break;
}
}
} else {
//Adding a new todo
viewDetailsTodo.hidden = YES;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment