Advertisement
Guest User

MySync.m

a guest
Feb 22nd, 2013
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  MySync.m
  3. // Attention:
  4.  
  5. //  MySync.h <NSURLConnectionDataDelegate>
  6. #define wsURL @"http://www.mydomain.com/api/"
  7.  
  8. #import "MySync.h"
  9. #import "Database.h"
  10. #import "ImageDownload.h"
  11.  
  12. @implementation MySync {
  13.     NSURLConnection *urlConnection;
  14.     NSMutableData *jsonData;
  15.    
  16.     MyViewController *myMainViewController;
  17.     UIProgressView *progressViewSync;
  18. }
  19.  
  20. - (void)start:(MyViewController *)myViewController progressView:(UIProgressView *)progressView
  21. {
  22.     myMainViewController = myViewController;
  23.     progressViewSync = progressView;
  24.    
  25.     NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", wsURL, @"sync"]]];
  26.     urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:TRUE];
  27. }
  28.  
  29. /** NSURLConnectionDataDelegate **/
  30. - (void)connection:(NSURLConnection *)urlConnection didReceiveResponse:(NSURLResponse *)response
  31. {
  32.     jsonData = [[NSMutableData alloc] init];
  33. }
  34.  
  35. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
  36. {
  37.     [jsonData appendData:data];
  38. }
  39.  
  40. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
  41. {
  42.     NSLog(@"Error during connection");
  43. }
  44.  
  45. - (void)connectionDidFinishLoading:(NSURLConnection *)connection
  46. {
  47.     NSLog(@"finish....");
  48.    
  49.     NSError *jsonParsingErrors = nil;
  50.     id object = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&jsonParsingErrors];
  51.    
  52.     if (jsonParsingErrors) {
  53.        
  54.     } else {
  55.        
  56.         //init sync
  57.         [myMainViewController syncJSonComplete];
  58.         [self syncImages:[object objectForKey:@"images"]];
  59.     }
  60. }
  61.  
  62.  
  63. /**
  64.  * custom methods
  65.  */
  66. - (void)syncImages:(NSArray *)images
  67. {
  68.     [[[ImageDownload alloc] init] start:images sync:self];
  69. }
  70.  
  71. - (void)downloadImagesComplete
  72. {
  73.     NSLog(@"all downloads complete");
  74.     [myMainViewController syncComplete];
  75. }
  76.  
  77. - (void)setProgressImageDownload:(float)progress
  78. {
  79.     [myMainViewController setProgressView:progress];
  80. }
  81.  
  82.  
  83. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement