I have a uiwebview
that sends its loaded page url to a uitableview
to retain a history of visited sites. It works great except that a few sites pause loading briefly which causes another url reference to be created. Try going to redmondpie.com and I end up with 50 redmondpie.com links in my visited list. Am I doing this wrong or is there a code I can use to block the link being recreated over and over while the page loads. I guess the best option would be something like if the url exists right next to the one being created then don't create it, or something. Heres how it's loaded now.
- (void)webViewDidFinishLoad:(UIWebView *)webView {
urlField.text = [[[[self webView]request] URL] absoluteString];
{
NSMutableArray *history = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"History"] mutableCopy];
if (!history) {
history = [[NSMutableArray alloc] init];
}
NSString *newString =
[urlField.text stringByReplacingOccurrencesOfString:@"http://" withString:@""]
urlField.text = [NSString stringWithFormat:@"%@", newString];
[history addObject:newString];
[[NSUserDefaults standardUserDefaults] setObject:history forKey:@"History"];
}
Check the last object in the array to see if it is the same before adding it by changing this:
[history addObject:newString];
[[NSUserDefaults standardUserDefaults] setObject:history forKey:@"History"];
to this:
if (![newString isEqualToString:(NSString *)[history lastObject]]) {
[history addObject:newString];
[[NSUserDefaults standardUserDefaults] setObject:history forKey:@"History"];
}