我可以在加载 UIWebview 时更改 URL 吗?(苹果)



我想在加载网页视图时更改URL,我可以这样做吗?例如:如果我发现URL是"www.google.com",我想更改它并继续加载它。我的代码是:

-(void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    self.webView.delegate = self;
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]];
}

但我不想这样做:

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)requestnavigationType:(UIWebViewNavigationType)navigationType
{
    if ([request.URL isEqual:@"www.google.com"]) {
        [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com.hk"]]];
        return NO;
    }
    return YES;
}

还有其他方法吗?谢谢。

您可以子类化UIWebView,并重新实现loadRequest方法,如果匹配,则进行更改。

最新更新