0

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:

Because the saved URL is always different from starting URL,I can't open saved URL, how can I deal with this?

HoangNA
  • 163
  • 1
  • 12
  • 1
    You can reference [here](http://stackoverflow.com/questions/7667463/check-if-uiwebview-is-loaded). You should search before ask question – dieuvn3b Dec 08 '15 at 09:04
  • @bluesky: that question doesn't help me find the opened URL. When I open a link in webview, the final link ( the one that I can save into array ) is not as same as the started link, so the comparison of the saved link vs the latest clicked link is always return FALSE. – HoangNA Dec 08 '15 at 09:13
  • You could store and compare the requests instead of just comparing the URLs. – Yedy Dec 08 '15 at 09:26

1 Answers1

0

save the url before the redirect // have this in your @interface

@property NSURL *urlBeingLoaded;

....

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if(navigationType == UIWebViewNavigationTypeLinkClicked) {
        if(![self checkIsAlreadyLoaded:request]) {
            // load new webview
            self.urlBeingLoaded = request.URL;
            [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:_urlBeingLoaded]) {
        NSLog(@"saved url: %@", _urlBeingLoaded);
        [self.visitedURLsArray addObject:self.urlBeingLoaded];
    }
}
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135