UIWebView does not load the URL

2020-06-27 14:43发布

I have very small problem while loading the URL in UIWebView. I have one UIButton, on clicking of it, I add the UIView which contains UIWebView, UIButton & Title bar. Code used is as follows -

    [self.view addSubview:vwOnline];
    vwOnline.bounds = self.view.bounds;

    //Load webview
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@", objWine.strOnlineURL]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [wbvwOnline loadRequest:request];

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    //Show activity indicator
    [indicator startAnimating];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    //Remove Activity indicator
    [indicator stopAnimating];
    [indicator removeFromSuperview];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"Error - %@", error);

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Find A Vino" message:@"Error while loading request." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
    alert.tag = 1;

    //Remove Activity indicator
    [indicator stopAnimating];
    [indicator removeFromSuperview];
}

By doing above code, most of times the UIWebView does not loads the URL which I passed in the object objWine.strOnlineURL. If I clicked the back button & again clicked on the button to load the URL, it goes into the - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error delegate method of UIWebView. If anyone knows the solution, then please help me.

3条回答
Ridiculous、
2楼-- · 2020-06-27 15:14

use

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.google.com"]];

if it is work fine then check objWine.strOnlineURL

otherwise it is clear :)

查看更多
▲ chillily
3楼-- · 2020-06-27 15:15

Instead of adding your webview on your button click each time. Why don't you add your webview into your viewDidLoad and hide and show it in your button click.

查看更多
\"骚年 ilove
4楼-- · 2020-06-27 15:21

when you are going back, make request to nil and load empty htmlstring to webview. One more thing, remove [indicator removeFromSuperview]; line from

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error 
-(void)webViewDidFinishLoad:(UIWebView *)webView
查看更多
登录 后发表回答