Dan Winter-Wijntjes
Dan Winter-Wijntjes

Reputation: 312

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;

Upvotes: 3

Views: 2827

Answers (2)

Lord Zsolt
Lord Zsolt

Reputation: 6557

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.

Upvotes: 4

Goppinath
Goppinath

Reputation: 10739

Hope you are having your logic in other than the method

- (void)viewDidLoad

move your code to - viewDidLoad

even if your are having trouble then pass the preloaded UIWebView when ever you are navigating.

Upvotes: 0

Related Questions