In my project I need to compare if a website changed from now to 30 minutes later, so after a research, I arrived in this:
- (IBAction)saveHTML:(id)sender{
// Determile cache file path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [NSString stringWithFormat:@"%@/%@", [paths objectAtIndex:0],@"index.html"];
self.Surl= [self.meuWebView stringByEvaluatingJavaScriptFromString:@"window.location.href"];
NSLog(@"URL: %@",self.Surl);
// Download and write to file
NSURL *url = [NSURL URLWithString:self.Surl];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
// Load file in UIWebView
// [web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];
self.oldURL = urlData;
self.timerURLNew = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(saveHTMLNew:) userInfo:[NSNumber numberWithBool:YES] repeats:NO];
}
-(void)saveHTMLNew:(id)sender{
// Determile cache file path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [NSString stringWithFormat:@"%@/%@", [paths objectAtIndex:0],@"index.html"];
self.Surl= [self.meuWebView stringByEvaluatingJavaScriptFromString:@"window.location.href"];
NSLog(@"%@",self.Surl);
NSURL *url = [NSURL URLWithString:self.Surl];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
self.newURL = urlData;
if (![self.oldURL isEqualToData:self.newURL]) {
NSLog(@"Something changed");
}
else {
NSLog(@"Nothing changed");
}
}
The problem is that everytime I get "Something Changed". For testing I create a blog to post something to change its HTML.