// // MySync.m // Attention: // MySync.h #define wsURL @"http://www.mydomain.com/api/" #import "MySync.h" #import "Database.h" #import "ImageDownload.h" @implementation MySync { NSURLConnection *urlConnection; NSMutableData *jsonData; MyViewController *myMainViewController; UIProgressView *progressViewSync; } - (void)start:(MyViewController *)myViewController progressView:(UIProgressView *)progressView { myMainViewController = myViewController; progressViewSync = progressView; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", wsURL, @"sync"]]]; urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:TRUE]; } /** NSURLConnectionDataDelegate **/ - (void)connection:(NSURLConnection *)urlConnection didReceiveResponse:(NSURLResponse *)response { jsonData = [[NSMutableData alloc] init]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [jsonData appendData:data]; } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"Error during connection"); } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSLog(@"finish...."); NSError *jsonParsingErrors = nil; id object = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&jsonParsingErrors]; if (jsonParsingErrors) { } else { //init sync [myMainViewController syncJSonComplete]; [self syncImages:[object objectForKey:@"images"]]; } } /** * custom methods */ - (void)syncImages:(NSArray *)images { [[[ImageDownload alloc] init] start:images sync:self]; } - (void)downloadImagesComplete { NSLog(@"all downloads complete"); [myMainViewController syncComplete]; } - (void)setProgressImageDownload:(float)progress { [myMainViewController setProgressView:progress]; } @end