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

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;
}

The delegate method of UIWebView :

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

returns YES if the web view should begin loading content otherwise it returns NO. The method is invoked in viewDidLoad method when you call :

[webView loadRequest:requestObj];

And in the delegate method you are again calling the same method :

[webView loadRequest:[NSURLRequest requestWithURL:url]];

This turns into calling the delegate methods to be called again and again and form an infinite loop. This happens to loadRequest each time but before it loads, the same loadRequest is called again, forming the loop.

There is no need to loadRequest in this method, simply return YES or NO for your conditions needed.

链接地址: http://www.djcxy.com/p/34786.html

上一篇: NSURLProtocol。 requestIsCacheEquivalent从未调用过

下一篇: UIWebView模拟器突然不工作