Advertisement
smugwimp

BT Report To Cloud

Oct 23rd, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //reportToCloud...
  2. -(void)reportToCloud{
  3.     [BT_debugger showIt:self message:[NSString stringWithFormat:@"reportToCloud%@", @""]];
  4.    
  5.     //if we do not have any reportToCloud results yet, make a fake one...
  6.     if(![BT_fileManager doesLocalFileExist:[self modifiedFileName]]){
  7.         [BT_fileManager saveTextFileToCacheWithEncoding:@"blankLastModified" fileName:self.modifiedFileName encodingFlag:-1];
  8.     }
  9.    
  10.    
  11.     //ignore if we are refreshing...
  12.     if(![self isRefreshing]){
  13.         NSString *useURL = @"";
  14.         //app's configuration data must have  a "dataURL" and a "reportToCloudURL"...
  15.         if([[self.rootApp dataURL] length] > 1 && [[self.rootApp reportToCloudURL] length] > 1){
  16.             useURL = [self.rootApp reportToCloudURL];
  17.         }else{
  18.             [BT_debugger showIt:self theMessage:[NSString stringWithFormat:@"not reporting to cloud, no dataURL or reportToCloudURL%@", @""]];
  19.         }
  20.         if ([useURL length] > 3) {
  21.             //if we have a currentMode in the BT_config.txt IN THE PROJECT, append it to the end of the URL...
  22.             if([[self currentMode] length] > 0){
  23.                 useURL = [useURL stringByAppendingString:[NSString stringWithFormat:@"&currentMode=%@", [self currentMode]]];
  24.             }
  25.             //the dataURL may contain merge fields...
  26.             [BT_debugger showIt:self theMessage:[NSString stringWithFormat:@"reporting to cloud at : %@", useURL]];
  27.             NSString *tmpURL = [BT_strings mergeBTVariablesInString:useURL];
  28.             NSURL *escapedURL = [NSURL URLWithString:[tmpURL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]]];
  29.            
  30.             ////////////////////////////
  31.             // start of replacement code
  32.             NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
  33.             NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];
  34.            
  35.             //            NSURL * url = [NSURL URLWithString:mySQLServerURL];
  36.             //            NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
  37.             NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
  38.             [request setURL:escapedURL];
  39.             [request setHTTPMethod:@"GET"];
  40.             NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest: request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
  41.                 /////////////
  42.                 [BT_debugger showIt:self theMessage:[NSString stringWithFormat:@"Response: %@ -|- %@\n", response, error]];
  43.                 /////////////
  44.                 if(error == nil) {
  45.                     [self doSomethingOK:data];
  46.                     NSString * returnText = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
  47.                     [BT_debugger showIt:self theMessage:[NSString stringWithFormat:@"Returned Data: %@", returnText]];
  48.                 }
  49.                 /////////////
  50.             }];
  51.             [dataTask resume];
  52.         }
  53.     }//isRefreshing...
  54.    
  55. }
  56. ////////////
  57. -(void) doSomethingOK:(NSData *)returnData {
  58.     //save data as "lastModified" file
  59.     NSString *dStringData = [[NSString alloc] initWithData:returnData encoding:NSASCIIStringEncoding];
  60.     if([dStringData length] > 3){
  61.        
  62.         //returned data format: {"lastModifiedUTC":"2011-02-22 02:13:25"}
  63.         NSString *lastModified = @"";
  64.         NSString *previousModified = @"";
  65.        
  66.         //parse returned JSON data
  67.         SBJsonParser *parser = [SBJsonParser new];
  68.         id jsonData = [parser objectWithString:dStringData];
  69.         if(jsonData){
  70.             if([jsonData objectForKey:@"lastModifiedUTC"]){
  71.                 lastModified = [jsonData objectForKey:@"lastModifiedUTC"];
  72.                 [BT_debugger showIt:self theMessage:[NSString stringWithFormat:@"lastModified (value on server): %@", lastModified]];
  73.             }
  74.         }
  75.        
  76.         //parse previous saved data
  77.         if([BT_fileManager doesLocalFileExist:self.modifiedFileName]){
  78.             NSString *previousData = [BT_fileManager readTextFileFromCacheWithEncoding:self.modifiedFileName encodingFlag:-1];
  79.             SBJsonParser *parser = [SBJsonParser new];
  80.             id jsonData = [parser objectWithString:previousData];
  81.             if(jsonData){
  82.                 if([jsonData objectForKey:@"lastModifiedUTC"]){
  83.                     previousModified = [jsonData objectForKey:@"lastModifiedUTC"];
  84.                     [BT_debugger showIt:self theMessage:[NSString stringWithFormat:@"previousModified (value on device): %@", previousModified]];
  85.                 }
  86.             }
  87.         }
  88.        
  89.         //save a copy of the lastModified text for next time..
  90.         BOOL saved = [BT_fileManager saveTextFileToCacheWithEncoding:dStringData fileName:self.modifiedFileName encodingFlag:-1];
  91.         if(saved){};
  92.        
  93.         //if value are not emtpy, and different....ask user to confirm refresh...
  94.         if([lastModified length] > 3 && [previousModified length] > 3){
  95.             if(![lastModified isEqualToString:previousModified]){
  96.                
  97.                 //show alert with confirmation...
  98.                 UIAlertController * alertView = [UIAlertController
  99.                                                  alertControllerWithTitle:@"Refresh Data"
  100.                                                  message:NSLocalizedString(@"updatesAvailable", "This app's content has changed, would you like to refresh?")
  101.                                                  preferredStyle:UIAlertControllerStyleAlert];
  102.                
  103.                 UIAlertAction* yesButton = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { [self clickedOnButton:12]; }];
  104.                 UIAlertAction* noButton = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { [self clickedOnButton:0]; }];
  105.                 [alertView addAction:yesButton];
  106.                 [alertView addAction:noButton];
  107.                 [self.rootApp.rootNavController  presentViewController:alertView animated:YES completion:nil];
  108.             }
  109.         }else{
  110.             [BT_debugger showIt:self theMessage:[NSString stringWithFormat:@"%@ does not exist in the cache. Not checking for updates.", self.modifiedFileName]];
  111.         }
  112.     }
  113. }
  114.  
  115.  
  116. -(void)clickedOnButton:(int)alertTag {
  117.    
  118.     if(alertTag == 0){
  119.         //do nothing...
  120.     }
  121.    
  122.     if(alertTag == 12){
  123.         [self refreshAppData];
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement