Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //download app data...
- -(void)downloadAppData{
- [BT_debugger showIt:self message:[NSString stringWithFormat:@"downloadAppData%@", @""]];
- //get a reference to the appDelegate...
- kprg_appDelegate *appDelegate = (kprg_appDelegate *)[[UIApplication sharedApplication] delegate];
- //start spinner...
- [self.spinner startAnimating];
- //the dataURL may contain merge fields...
- NSString *tmpURL = [BT_strings mergeBTVariablesInString:[appDelegate.rootApp dataURL]];
- //clean up URL
- NSString *escapedUrl = [tmpURL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
- [BT_debugger showIt:self theMessage:[NSString stringWithFormat:@"downloading screen data from: %@", escapedUrl]];
- NSString *myFile = escapedUrl;
- NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
- NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];
- NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithURL:[NSURL URLWithString:myFile]];
- [downloadTask resume];
- }
- // Finished downloading; fire off whatever happens after...
- - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
- NSData *data = [NSData dataWithContentsOfURL:location];
- dispatch_async(dispatch_get_main_queue(), ^{
- //
- [BT_fileManager saveDataToFile:data fileName:saveAsFileName];
- NSString *dataString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
- // [self parseScreenData:dataString];
- [BT_debugger showIt:self message:@"Finished Download."];
- [BT_debugger showIt:self message:[NSString stringWithFormat:@"Downloaded Info: %@: ", dataString]];
- [self hideProgress];
- //get a reference to the appDelegate...
- kprg_appDelegate *appDelegate = (kprg_appDelegate *)[[UIApplication sharedApplication] delegate];
- //save the version we just downloaded...
- if([BT_fileManager saveTextFileToCacheWithEncoding:dataString fileName:[self saveAsFileName] encodingFlag:-1]){
- //the data we just got must be valid
- if([appDelegate.rootApp validateApplicationData:dataString]){
- //delete previously cached data (this does not remove a cached JSON config file)...
- [BT_fileManager deleteAllLocalData];
- //parse newly downloaded data...
- [appDelegate.rootApp parseJSONData:dataString];
- //flag as not needing refreshed anymore...
- [self setNeedsRefreshed:FALSE];
- //rebuild environment...
- [self configureEnvironmentUsingAppData:dataString];
- }else{
- //remove bogus download...
- [BT_fileManager deleteFile:self.saveAsFileName];
- [BT_debugger showIt:self message:[NSString stringWithFormat:@"error parsing downloaded app config data: %@", @"fuge"]];
- [self showAlert:nil message:NSLocalizedString(@"appDownloadErrorInvalidJSON", @"There was a problem reading some required data. Try again?") alertTag:68];
- [self setNeedsRefreshed:TRUE];
- }
- }else{
- [BT_debugger showIt:self message:[NSString stringWithFormat:@"error saving downloaded app config data%@", @""]];
- [self showAlert:nil message:NSLocalizedString(@"appDownloadErrorCouldNotSave", @"There was a problem saving some required data. Try again?") alertTag:68];
- [self setNeedsRefreshed:TRUE];
- }
- });
- }
- - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes {
- // Real Men don't pause downloads
- }
- - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
- // progress = percentage complete. Use the value, or don't.
- // float progress = (double)totalBytesWritten / (double)totalBytesExpectedToWrite;
- dispatch_async(dispatch_get_main_queue(), ^{
- // update any progress bars here
- });
- }
- //////////////////////////////////////////////////////////////////
Add Comment
Please, Sign In to add comment