smugwimp

Download App Data

Oct 23rd, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. //download app data...
  3. -(void)downloadAppData{
  4.     [BT_debugger showIt:self message:[NSString stringWithFormat:@"downloadAppData%@", @""]];
  5.    
  6.     //get a reference to the appDelegate...
  7.     kprg_appDelegate *appDelegate = (kprg_appDelegate *)[[UIApplication sharedApplication] delegate];
  8.    
  9.     //start spinner...
  10.     [self.spinner startAnimating];
  11.    
  12.     //the dataURL may contain merge fields...
  13.     NSString *tmpURL = [BT_strings mergeBTVariablesInString:[appDelegate.rootApp dataURL]];
  14.    
  15.     //clean up URL
  16.     NSString *escapedUrl = [tmpURL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
  17.    
  18.     [BT_debugger showIt:self theMessage:[NSString stringWithFormat:@"downloading screen data from: %@", escapedUrl]];
  19.     NSString *myFile = escapedUrl;
  20.     NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
  21.     NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];
  22.     NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithURL:[NSURL URLWithString:myFile]];
  23.     [downloadTask resume];
  24. }
  25.  
  26. // Finished downloading; fire off whatever happens after...
  27. - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
  28.     NSData *data = [NSData dataWithContentsOfURL:location];
  29.     dispatch_async(dispatch_get_main_queue(), ^{
  30.         //
  31.        
  32.         [BT_fileManager saveDataToFile:data fileName:saveAsFileName];
  33.         NSString *dataString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
  34.         //        [self parseScreenData:dataString];
  35.        
  36.         [BT_debugger showIt:self message:@"Finished Download."];
  37.         [BT_debugger showIt:self message:[NSString stringWithFormat:@"Downloaded Info: %@: ", dataString]];
  38.        
  39.         [self hideProgress];
  40.        
  41.         //get a reference to the appDelegate...
  42.         kprg_appDelegate *appDelegate = (kprg_appDelegate *)[[UIApplication sharedApplication] delegate];
  43.         //save the version we just downloaded...
  44.         if([BT_fileManager saveTextFileToCacheWithEncoding:dataString fileName:[self saveAsFileName] encodingFlag:-1]){
  45.            
  46.             //the data we just got must be valid
  47.             if([appDelegate.rootApp validateApplicationData:dataString]){
  48.                
  49.                 //delete previously cached data (this does not remove a cached JSON config file)...
  50.                 [BT_fileManager deleteAllLocalData];
  51.                
  52.                 //parse newly downloaded data...
  53.                 [appDelegate.rootApp parseJSONData:dataString];
  54.                
  55.                 //flag as not needing refreshed anymore...
  56.                 [self setNeedsRefreshed:FALSE];
  57.                
  58.                 //rebuild environment...
  59.                 [self configureEnvironmentUsingAppData:dataString];
  60.                
  61.                
  62.             }else{
  63.                
  64.                 //remove bogus download...
  65.                 [BT_fileManager deleteFile:self.saveAsFileName];
  66.                
  67.                 [BT_debugger showIt:self message:[NSString stringWithFormat:@"error parsing downloaded app config data: %@", @"fuge"]];
  68.                 [self showAlert:nil message:NSLocalizedString(@"appDownloadErrorInvalidJSON", @"There was a problem reading some required data. Try again?") alertTag:68];
  69.                 [self setNeedsRefreshed:TRUE];
  70.                
  71.             }
  72.            
  73.         }else{
  74.            
  75.             [BT_debugger showIt:self message:[NSString stringWithFormat:@"error saving downloaded app config data%@", @""]];
  76.             [self showAlert:nil message:NSLocalizedString(@"appDownloadErrorCouldNotSave", @"There was a problem saving some required data. Try again?") alertTag:68];
  77.             [self setNeedsRefreshed:TRUE];
  78.            
  79.         }
  80.     });
  81. }
  82.  
  83. - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes {
  84.     // Real Men don't pause downloads
  85. }
  86.  
  87. - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
  88.    
  89.     //    progress = percentage complete. Use the value, or don't.
  90.     //    float progress = (double)totalBytesWritten / (double)totalBytesExpectedToWrite;
  91.     dispatch_async(dispatch_get_main_queue(), ^{
  92.         // update any progress bars here
  93.     });
  94. }
  95. //////////////////////////////////////////////////////////////////
Add Comment
Please, Sign In to add comment