A possibility is to consume shouldStartLoadWithRequest
.
You should:
- Get the url from request.
- Send an asynchronous request to the url.
- Check if you get response. If not response cancel this request otherwise allow it by returning
TRUE
.
Have a look at how you can send request.
Checking reachability again a specific page
Is it wise to use Reachability to check for a remote host's availability
Using this way you can cancel request and inform user that the specified page is not available. You can also check for HTTP Status Code of response which is usually 200, if you have a valid response.
Hope it helps!
Another way is to handle it from your webpage itself, i.e., redirect it to some common page that says the page is unavailable and a back
to redirect it to the previous page.
EDIT:
Try this code in separate project h
file:
@interface ViewController : UIViewController<UIWebViewDelegate>
and in m
file:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]];
UIWebView *wbWebView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.view addSubview:wbWebView];
[wbWebView setDelegate:self]; // <------------- set delegate
[wbWebView loadRequest:request];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(@"request loading... %@", [request URL].absoluteString);
// TODO : Validate here
// If validated then return TRUE otherwise false;
return YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSLog(@"request finished loading");
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(@"request failed loading");
}