UIWebView覆盖url

时间:2014-07-16 18:14:58

标签: ios uiwebview swift

我想创建一个iPhone WebView应用程序。但是有一个问题,如果我点击一些按钮,它会打开正常而不是移动版本。

我确实在Android上创建了相同的WebView应用程序,在那里我使用此代码解决了问题

webView.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) 
        {
            if(!url.toLowerCase().contains("http://www.example.com/"))
            {
                webView.loadUrl(url + "?mt=1");
                return true;
            }
        return false;
            }
        });

如何在xcode上执行此操作?

非常感谢

祝福

多米尼克

1 个答案:

答案 0 :(得分:0)

您可以使用UIWebViewDelegate协议执行相同操作。

我认为您可以通过在委托函数中更改NSURLRequest来获得所需的结果 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)请求.......

//delegate methods
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{


    //use NSURLRequest object request , to manage the request.
 //use NSURLRequest object request , to manage the request.
    NSURL *urL=request.URL;
    NSString *urlStr=[urL absoluteString];
    NSLog(@"URLL %@",urlStr);
    if([urlStr isEqualToString:@"http://www.google.com/"]){
        urL=[NSURL URLWithString:@"http://www.yahoo.com"];
        request=[request initWithURL:urL];
        [webView loadRequest:request];
    }

    return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    //start of request
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    //finished loading
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

}




 - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
    {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        //error
    }

这肯定会奏效。当请求www.google.com时,我使用上面的代码加载www.yahoo.com。

相关问题