1

I have a UI webView in my app works fine if I press reload the web view is reloaded so all is good so far,

Lets say I turn off my internet and load my app and web page is blank, now I reconnect to internet and open my app reload the page but my page remains blank and does not load the page.

Now to make my app working I have to exit my app and restart the app but this is not handy.

Please help.

Venky
  • 509
  • 7
  • 25
Moneeb
  • 441
  • 1
  • 4
  • 6

2 Answers2

1

Try this code snippet

 - (void)viewWillAppear:(BOOL)animated {
    [webView reload]; //your webview instance 
    }
Venky
  • 509
  • 7
  • 25
  • Thanks butt still doesn't work the webview is still blank once it is loaded without internet connection it doesn't load page even after internet is available. – Moneeb Aug 24 '14 at 17:13
  • @Moneeb try this [[NSURLCache sharedURLCache] removeAllCachedResponses]; you could find more info about it here http://blog.techno-barje.fr/post/2010/10/04/UIWebView-secrets-part2-leaks-on-release/ – Venky Aug 25 '14 at 05:46
1

You will need to detect the network status changes of your device. This answer(Method 2) will show you step-by-step of how you can do that. The checkNetworkStatus method will get called every time if the device's is changed from WiFi to 3G, or from WiFi to not being connected to the internet.

To reload your UIWebView when the network status changes. Your checkNetworkStatus should look something like this:

-(void) checkNetworkStatus:(NSNotification *)notice
{
    // called after network status changes
    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    switch (internetStatus)
    {
        case NotReachable:
        {
            NSLog(@"The internet is down.");
            self.internetActive = NO;

            //show an alertView saying the device has no internet connection.

            break;
        }
        case ReachableViaWiFi:
        {
            NSLog(@"The internet is working via WIFI.");
            self.internetActive = YES;

            //reload UIWebView
            NSURLRequest *nsrequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]];
            [self.webView loadRequest:nsrequest];

            break;
        }
        case ReachableViaWWAN:
        {
            NSLog(@"The internet is working via WWAN.");
            self.internetActive = YES;

            //reload UIWebView
            NSURLRequest *nsrequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]];
            [self.webView loadRequest:nsrequest];

            break;
        }
    }

    // anything else
} 
Community
  • 1
  • 1
Ty Lertwichaiworawit
  • 2,930
  • 2
  • 20
  • 41
  • thanks I hope it will work for me but there is a slight error when I implement the code it says "undeclared identifier Network Status" both NetworkStatus and internet status are undeclared, do I have to import some library in my .h file first ? – Moneeb Aug 25 '14 at 09:23
  • @Moneeb did you import Reachability class like this answer told you to? http://stackoverflow.com/a/3597085/2053537 follow every step provided by the answer. – Ty Lertwichaiworawit Aug 25 '14 at 09:26
  • no need to include systemconfiguration framework, just include the reachability – Ty Lertwichaiworawit Aug 25 '14 at 09:27
  • I implemented everything exactly step by step but its strange that the problem exists, NS log works fine it show if internet is working or its down but UI webview doesn't load the page once its loaded without internet. – Moneeb Aug 25 '14 at 09:45
  • @Moneeb so the method gets called when the internet was turned on and when its turned off right? – Ty Lertwichaiworawit Aug 25 '14 at 10:11
  • you created you UIWebView programmatically or in storyboard? – Ty Lertwichaiworawit Aug 25 '14 at 10:12
  • yess method gets called whenever the internet is turned on or off, I created webview in storyboard – Moneeb Aug 25 '14 at 10:15
  • @Moneeb change my example of google.com to your website. And I'd appreciate it if you accept the answer if the code works. Thanks – Ty Lertwichaiworawit Aug 25 '14 at 10:40