UIWebView模拟器突然不工作

我有一套这样的代码在大约一个小时之前运行良好。 然后,当我尝试再次启动它时,UIWebView只显示一个白色屏幕。 但是如果现在我阻止了 - (bool)方法,viewDidLoad将会发生。 (用NSLog测试过)请问代码发生了什么? 它正在工作,突然停止运作。

.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];

}

如果我实现上面的方法,应用程序时,打开UIWebView将不断弹出说错误域错误-999不停止。

- (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

加载请求时,委托方法

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

将会被调用。

在该方法中,您正试图无限次地重新加载相同的页面。

错误域= NSURLErrorDomain代码= -999“操作无法完成。(NSURLErrorDomain错误-999。)”UserInfo = 0xe203e80 {NSErrorFailingURLKey = http://www.imc.jhmi.edu/news.html,NSErrorFailingURLStringKey = http ://www.imc.jhmi.edu/news.html}

我试过你的代码,并得到这个错误。 如果在WebView的先前请求完成之前发生另一个请求,则可能会发生此错误。 所以请尽量避免这些委托方法中的条件重复加载请求。

if(range2.location==NSNotFound)

这种情况将永远是真的。

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

UIWebView的委托方法:

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

如果Web视图应该开始加载内容,则返回YES,否则返回NO。 调用时,该方法在viewDidLoad方法中调用:

[webView loadRequest:requestObj];

在委托方法中,您再次调用相同的方法:

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

这变成调用委托方法被反复调用并形成无限循环。 这发生在loadRequest每次加载之前,再次调用相同的loadRequest,形成循环。

无需在此方法中加载请求,只需为您所需的条件返回YES或NO。

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

上一篇: UIWebView simulator suddenly not working

下一篇: In a storyboard, how do I make a custom cell for use with multiple controllers?