webView - how to stop reloading

cocoa-touch, ios, uiwebview

Solution

Your webview is reloading every time you go to that screen is probably because you added it to either viewDidAppear: or viewWillAppear.

Add your block of code to the viewDidLoad method so it only gets executed when the view is loaded (aka when it's shown for the first time).

- (void)viewDidLoad:(BOOL)animated {
     NSURL *webUrl = [NSURL URLWithString:@"myurl.com"];
     NSURLRequest *webrequestUrl = [NSURLRequest requestWithURL:webUrl];
     [webView loadRequest:webrequestUrl];

      webView.scrollView.scrollEnabled = NO;
      webView.scrollView.bounces = NO;
}

Edit: Oh by going to another view and back you meant going back on the navigation stack (or switching the navigation stack). In that case, you can keep a strong reference to your view controller with the webview and reuse it. Though I wouldn't suggest it doing it this was, unless that screen is the most important screen of your application.

Problem

I am developing an app in Xcode but am having trouble with one part. Basicly on the main app page, there is a small section that contains a web view. Everytime I change to another view controller and back, i see it flicker and reload. Is there a way to prevent it from reloading every time i open the view? instead just reloading every time I open the app. This is the code: ``` NSURL *webUrl = [NSURL URLWithString:@"myurl.com"]; NSURLRequest *webrequestUrl = [NSURLRequest requestWithURL:webUrl]; [webView loadRequest:webrequestUrl]; webView.scrollView.scrollEnabled = NO; webView.scrollView.bounces = NO; ```

Original source