Guest User

BT Download Data Methods...

a guest
Sep 29th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ///////////////////////////////////////////////////////////////
  2. ///////////////////////////////////////////////////////////////
  3. //download data via NSURLSession
  4. ///////////////////////////////////////////////////////////////
  5. ///////////////////////////////////////////////////////////////
  6. -(void)downloadTheData {
  7.     [BT_debugger showIt:self theMessage:[NSString stringWithFormat:@"downloading screen data from: %@", @"beekdata.txt"]];
  8.     NSString *myFile = [BT_strings getJsonPropertyValue:self.screenData.jsonVars nameOfProperty:@"postURL" defaultValue:@""];
  9.     NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
  10.     NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];
  11.     NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithURL:[NSURL URLWithString:myFile]];
  12.     [downloadTask resume];
  13. }
  14.  
  15. // Finished downloading; fire off whatever happens after...
  16. - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
  17.     NSData *data = [NSData dataWithContentsOfURL:location];
  18.     dispatch_async(dispatch_get_main_queue(), ^{
  19.         //
  20.  
  21.         [BT_fileManager saveDataToFile:data fileName:saveAsFileName];
  22.         NSString *dataString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
  23.         [self parseScreenData:dataString];
  24.        
  25.         [BT_debugger showIt:self message:@"Finished Download."];
  26.         [BT_debugger showIt:self message:[NSString stringWithFormat:@"Downloaded Info: %@: ", dataString]];
  27.         //
  28.     });
  29. }
  30.  
  31. - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes {
  32.     // Real Men don't pause downloads
  33. }
  34.  
  35. - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
  36.    
  37.     //    progress = percentage complete. Use the value, or don't.
  38.     //    float progress = (double)totalBytesWritten / (double)totalBytesExpectedToWrite;
  39.     dispatch_async(dispatch_get_main_queue(), ^{
  40.         // update any progress bars here
  41.     });
  42. }
  43. //////////////////////////////////////////////////////////////////
  44. /// End of Download Methods from NSURLSession
Add Comment
Please, Sign In to add comment