Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //reportToCloud...
- -(void)reportToCloud{
- [BT_debugger showIt:self message:[NSString stringWithFormat:@"reportToCloud%@", @""]];
- //if we do not have any reportToCloud results yet, make a fake one...
- if(![BT_fileManager doesLocalFileExist:[self modifiedFileName]]){
- [BT_fileManager saveTextFileToCacheWithEncoding:@"blankLastModified" fileName:self.modifiedFileName encodingFlag:-1];
- }
- //ignore if we are refreshing...
- if(![self isRefreshing]){
- NSString *useURL = @"";
- //app's configuration data must have a "dataURL" and a "reportToCloudURL"...
- if([[self.rootApp dataURL] length] > 1 && [[self.rootApp reportToCloudURL] length] > 1){
- useURL = [self.rootApp reportToCloudURL];
- }else{
- [BT_debugger showIt:self theMessage:[NSString stringWithFormat:@"not reporting to cloud, no dataURL or reportToCloudURL%@", @""]];
- }
- if ([useURL length] > 3) {
- //if we have a currentMode in the BT_config.txt IN THE PROJECT, append it to the end of the URL...
- if([[self currentMode] length] > 0){
- useURL = [useURL stringByAppendingString:[NSString stringWithFormat:@"¤tMode=%@", [self currentMode]]];
- }
- //the dataURL may contain merge fields...
- [BT_debugger showIt:self theMessage:[NSString stringWithFormat:@"reporting to cloud at : %@", useURL]];
- NSString *tmpURL = [BT_strings mergeBTVariablesInString:useURL];
- NSURL *escapedURL = [NSURL URLWithString:[tmpURL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]]];
- ////////////////////////////
- // start of replacement code
- NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
- NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];
- // NSURL * url = [NSURL URLWithString:mySQLServerURL];
- // NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
- NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
- [request setURL:escapedURL];
- [request setHTTPMethod:@"GET"];
- NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest: request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
- /////////////
- [BT_debugger showIt:self theMessage:[NSString stringWithFormat:@"Response: %@ -|- %@\n", response, error]];
- /////////////
- if(error == nil) {
- [self doSomethingOK:data];
- NSString * returnText = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
- [BT_debugger showIt:self theMessage:[NSString stringWithFormat:@"Returned Data: %@", returnText]];
- }
- /////////////
- }];
- [dataTask resume];
- }
- }//isRefreshing...
- }
- ////////////
- -(void) doSomethingOK:(NSData *)returnData {
- //save data as "lastModified" file
- NSString *dStringData = [[NSString alloc] initWithData:returnData encoding:NSASCIIStringEncoding];
- if([dStringData length] > 3){
- //returned data format: {"lastModifiedUTC":"2011-02-22 02:13:25"}
- NSString *lastModified = @"";
- NSString *previousModified = @"";
- //parse returned JSON data
- SBJsonParser *parser = [SBJsonParser new];
- id jsonData = [parser objectWithString:dStringData];
- if(jsonData){
- if([jsonData objectForKey:@"lastModifiedUTC"]){
- lastModified = [jsonData objectForKey:@"lastModifiedUTC"];
- [BT_debugger showIt:self theMessage:[NSString stringWithFormat:@"lastModified (value on server): %@", lastModified]];
- }
- }
- //parse previous saved data
- if([BT_fileManager doesLocalFileExist:self.modifiedFileName]){
- NSString *previousData = [BT_fileManager readTextFileFromCacheWithEncoding:self.modifiedFileName encodingFlag:-1];
- SBJsonParser *parser = [SBJsonParser new];
- id jsonData = [parser objectWithString:previousData];
- if(jsonData){
- if([jsonData objectForKey:@"lastModifiedUTC"]){
- previousModified = [jsonData objectForKey:@"lastModifiedUTC"];
- [BT_debugger showIt:self theMessage:[NSString stringWithFormat:@"previousModified (value on device): %@", previousModified]];
- }
- }
- }
- //save a copy of the lastModified text for next time..
- BOOL saved = [BT_fileManager saveTextFileToCacheWithEncoding:dStringData fileName:self.modifiedFileName encodingFlag:-1];
- if(saved){};
- //if value are not emtpy, and different....ask user to confirm refresh...
- if([lastModified length] > 3 && [previousModified length] > 3){
- if(![lastModified isEqualToString:previousModified]){
- //show alert with confirmation...
- UIAlertController * alertView = [UIAlertController
- alertControllerWithTitle:@"Refresh Data"
- message:NSLocalizedString(@"updatesAvailable", "This app's content has changed, would you like to refresh?")
- preferredStyle:UIAlertControllerStyleAlert];
- UIAlertAction* yesButton = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { [self clickedOnButton:12]; }];
- UIAlertAction* noButton = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { [self clickedOnButton:0]; }];
- [alertView addAction:yesButton];
- [alertView addAction:noButton];
- [self.rootApp.rootNavController presentViewController:alertView animated:YES completion:nil];
- }
- }else{
- [BT_debugger showIt:self theMessage:[NSString stringWithFormat:@"%@ does not exist in the cache. Not checking for updates.", self.modifiedFileName]];
- }
- }
- }
- -(void)clickedOnButton:(int)alertTag {
- if(alertTag == 0){
- //do nothing...
- }
- if(alertTag == 12){
- [self refreshAppData];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement