0

In my app I have 2 UIWebView's and 2 Adress Bar tat called WebView and WebView2, webAdress and webAdress2. I need to get url from WebView and put it to webAdress, and from WebView2 and put it to webAdress2.

When I use this code, the URL updates appears only in first webAdress, url from WebView2 apperas in first webAdress too. Moreover, all pages from WebView2 starts to be load in WebView.

    - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
        //CAPTURE USER LINK-CLICK.
        if (navigationType == UIWebViewNavigationTypeLinkClicked) {
            NSURL *URL = [request URL]; 
            if ([[URL scheme] isEqualToString:@"http"]) {
                [webAdress setText:[URL absoluteString]];
                [self gotoAddress:nil];
            }    
            return NO;
        }   
        return YES;   
    }

- (BOOL)webView2:(UIWebView*)webView2 shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
        //CAPTURE USER LINK-CLICK.
        if (navigationType == UIWebViewNavigationTypeLinkClicked) {
            NSURL *URL = [request URL]; 
            if ([[URL scheme] isEqualToString:@"http"]) {
                [webAdress2 setText:[URL absoluteString]];
                [self gotoAddress2:nil];
            }    
            return NO;
        }   
        return YES;   
    }
nik1004
  • 327
  • 3
  • 11

1 Answers1

2

I guess you are in need of only one delegate method. Check which webview has triggered this delegate method and perform actions depend on this:

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
        //CAPTURE USER LINK-CLICK.
        if (navigationType == UIWebViewNavigationTypeLinkClicked) {
            NSURL *URL = [request URL]; 
            if ([[URL scheme] isEqualToString:@"http"]) {
                if (webView == webView1)
                     [webAdress setText:[URL absoluteString]];
                if (webView == webView2)
                     [webAdress2 setText:[URL absoluteString]];
                [self gotoAddress2:nil];
            }    
            return NO;
        }   
        return YES;   
    }

Just set for all web views delegate as self, and all you can handle all actions in this method.

beryllium
  • 29,669
  • 15
  • 106
  • 125
  • I put outlets to delegate for WebView, WebView2, webAdress, webAdress2. – nik1004 Dec 01 '11 at 16:06
  • And when i try to use your code, xcode gives me errors. At this line if (webView == webView1) 2 errors: Local declaration of 'webView' hides instance variable and Use of undeclared identifier 'webView1' And at this line if (webView == webView2) it says Local declaration of 'webView' hides instance variable – nik1004 Dec 01 '11 at 16:08
  • 1
    Then just rename the argument in the method provided to a different name. Something other than `webView`. – sudo rm -rf Dec 01 '11 at 16:10
  • 1
    because you haven't webView1). rename it. – beryllium Dec 01 '11 at 16:12