UIWebView does not load the URL

怎甘沉沦 提交于 2019-12-05 04:57:28

问题


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.


回答1:


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



回答2:


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.




回答3:


use

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

if it is work fine then check objWine.strOnlineURL

otherwise it is clear :)



来源:https://stackoverflow.com/questions/14256484/uiwebview-does-not-load-the-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!