Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webView - how to stop reloading

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;
like image 926
Dan Winter-Wijntjes Avatar asked Apr 18 '26 18:04

Dan Winter-Wijntjes


1 Answers

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.

like image 151
Lord Zsolt Avatar answered Apr 20 '26 08:04

Lord Zsolt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!