I have a static webpage with about 10 links (items). My need is simple: when user clicked on a link, open in a new webview. When user back to menu, click again on this link, load the opened webview before (no need to add new webview with same url). Here's my solution: save loaded url and check this on pre-loading of new click.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if(navigationType == UIWebViewNavigationTypeLinkClicked) {
if(![self checkIsAlreadyLoaded:request]) {
// load new webview
[self addWebView:request];
return NO;
}
}
return YES;
}
- (BOOL)checkIsAlreadyLoaded:(NSURLRequest *)request {
NSString *urlToCheck = request.URL.absoluteString;
NSLog(@"URL to check: %@", urlToCheck);
for (int i = 0; i < self.visitedURLsArray.count; i++) {
if([[self.visitedURLsArray objectAtIndex:i] isEqualToString:request.URL.absoluteString]) {
NSLog(@"URL Found!");
return YES;
}
}
NSLog(@"*** NOT MATCHING ***");
return NO;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
if(![self.visitedURLsArray containsObject:webView.request.URL.absoluteString]) {
NSLog(@"saved url: %@", webView.request.URL.absoluteString);
[self.visitedURLsArray addObject:webView.request.URL.absoluteString];
}
}
Some of my links are Google link and I have a problems with these links. Ex: Google+ URL:
- starting URL: https://plus.google.com/app/basic?nopromo=1&source=mog&gl=jp
- final loaded URL: https://plus.google.com/collections/featured?gl=jp
Because the saved URL is always different from starting URL,I can't open saved URL, how can I deal with this?