Search code examples
objective-ciosuiwebview

UIWebView simulator suddenly not working


I have this set of code which was working fine about an hour or so ago. Then when I tried to launch it again few min back, the UIWebView just display a white screen. But if now I blocked out the -(bool) method, the viewDidLoad will occur. (tested with the NSLog) May I ask what happened to the code?? It was working and suddenly it stop functioning.

.m

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {

    NSString *titleString = @"Error Loading Page";
    NSString *messageString = [error localizedDescription];
    NSString *moreString = [error localizedFailureReason] ?
    [error localizedFailureReason] :
    NSLocalizedString(@"Try typing the URL again.", nil);
    messageString = [NSString stringWithFormat:@"%@. %@", messageString, moreString];

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:titleString
                                                        message:messageString delegate:self
                                              cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
    [alertView show];

}

if i implement the above method, the app when open UIWebView will constant pop up saying error domain error -999 non stop.

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString *currentURL = [[request URL] absoluteString];
    NSRange range1= [currentURL rangeOfString:@"news"];
    NSRange range2= [currentURL rangeOfString:@"pdf"];
    if (range1.location == NSNotFound)
    {
        NSURL *url = [NSURL URLWithString:@"http://www.imc.jhmi.edu/news.html"];
        [webView loadRequest:[NSURLRequest requestWithURL:url]];

        return YES;
    }
    if(range2.location==NSNotFound)
    {
        NSURL * url = [NSURL URLWithString:@"http://www.imc.jhmi.edu/news.html"];
        [webView loadRequest:[NSURLRequest requestWithURL:url]];
        return YES;
    }

    return NO;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    webView.scalesPageToFit=YES;
    webView.delegate = self;

    NSString *urlAddress = @"http://www.imc.jhmi.edu/news.html";
    NSURL *url =[NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [webView loadRequest:requestObj];
    NSLog(@"sadfasdf");
}

.h @interface ViewController : UIViewController{ IBOutlet UIWebView*webView; }

@property(nonatomic,strong) UIWebView*webView;

@end

Solution

  • When you load a request, the delegate method

    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
    

    will get called.

    In that method, you are trying to reload the same page for infinite number of times.

    Error Domain=NSURLErrorDomain Code=-999 "The operation couldn’t be completed. (NSURLErrorDomain error -999.)" UserInfo=0xe203e80 {NSErrorFailingURLKey=http://www.imc.jhmi.edu/news.html, NSErrorFailingURLStringKey=http://www.imc.jhmi.edu/news.html}

    I tried your code and got this error. This error may occur if another request is made before the previous request of the WebView is completed. So try to avoid those conditions in the delegate method repeating loading request.

    if(range2.location==NSNotFound)
    

    This condition will be always true.

    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
    {
        NSString *currentURL = [[request URL] absoluteString];
        NSRange range1= [currentURL rangeOfString:@"news"];
        if (range1.location == NSNotFound)
        {
            NSURL *url = [NSURL URLWithString:@"http://www.imc.jhmi.edu/news.html"];
            [webView loadRequest:[NSURLRequest requestWithURL:url]];
            return YES;
        }
    
        return YES;
    }