Question

I have a web view based program I was just testing. How do you display a image when there is no internet connection?

I have a basic webview

    //
//  XYZViewController.m
//  Phantomore
//
//  Created by Kevin Jin on 5/15/14.
//  Copyright (c) 2014 Kevin Jin. All rights reserved.
//

#import "XYZViewController.h"

@interface XYZViewController ()

@end

@implementation XYZViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *fullURL = @"http://www.phantomore.com";
    NSURL *url = [NSURL URLWithString:fullURL];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [_viewWeb loadRequest:requestObj];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

@end

Thats it,I just want to know what I have to do to have it displaying a image when tehre is no internet connection

Was it helpful?

Solution

I just simplified mamnun answer(Get Reachability here).

You can use this method after you have implemented the reachability class

- (BOOL)isConnectedToInternet
{
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus networkStatus = [reachability currentReachabilityStatus];
    return !(networkStatus == NotReachable);
}

In any of your class

- (void)viewDidLoad
{
    [super viewDidLoad];

    if([self isConnectedToInternet]){
       //Hide your imageview
       NSString *fullURL = @"http://www.phantomore.com";
       NSURL *url = [NSURL URLWithString:fullURL];
       NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
       [_viewWeb loadRequest:requestObj];
    }
   else{
       // Hide your webview
       // Show your imageview
       // Do not forget to import reachability class on top
   }
}

OTHER TIPS

There is two way of doing it.

  1. Before even performing the url request, check if internet connection is available. Use Apple provided Reachability classes or better yet use the open-source version. If internet is available load the request. If not add an UIImageView with your preferred UIImage as a subview of the webview. You can do something like this:

    //inside @interface
    @property(nonatomic, strong) UIImageView *imageView;
    
    //inside -(void)viewDidLoad
    self.imageView = [UIImageView alloc] initWithImage:[UIImage imageNamed:@"as.as"];
    Reachability* reach = [Reachability reachabilityWithHostname:@"www.google.com"];
    reach.reachableBlock = ^(Reachability*reach) {
        // load request
    NSString *fullURL = @"http://www.phantomore.com";
        NSURL *url = [NSURL URLWithString:fullURL];
        NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
        [_viewWeb loadRequest:requestObj];
    };
    
    reach.unreachableBlock = ^(Reachability*reach) {
        //show imageview
        [_viewWeb addSubView:self.imageView];
    };
    [reach startNotifier];
    
  2. Implement UIWebView delegate. In the – webView:didFailLoadWithError: method, check the error. If the error is caused by no internet connection, add the imageview(same as before).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top