【问题标题】:UIWebView simulator suddenly not workingUIWebView 模拟器突然不工作了
【发布时间】:2012-06-28 03:48:29
【问题描述】:

我有这组代码,大约一个小时前运行良好。然后当我试图再次启动它几分钟后,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 @接口视图控制器:UIViewController{ IBOutlet UIWebView*webView; }

@property(nonatomic,strong) UIWebView*webView;

@end

【问题讨论】:

    标签: objective-c ios uiwebview


    【解决方案1】:

    当你加载一个请求时,委托方法

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

    会被调用。

    在该方法中,您尝试无限次地重新加载同一页面。

    Error Domain=NSURLErrorDomain Code=-999 "操作无法完成。(NSURLErrorDomain error -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;
    }
    

    【讨论】:

    • 是的,我注意到了。但是如果我删除了 range2 部分,webview 仍然不会加载。只有当我删除所有内容并将其保留为 return yes; 时,才会加载 webview
    • 请像我的答案中的代码一样转换该委托方法,它对我有用。 :)
    • 如果我为我的 range2 添加另一个 if 语句,它似乎停止工作。但如果只将其保留为 range1,效果会很好
    【解决方案2】:

    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,从而形成循环。

    这个方法不需要loadRequest,只要根据你需要的条件返回YES或NO即可。

    【讨论】:

      猜你喜欢
      • 2020-09-23
      • 2014-08-05
      • 2018-01-19
      • 1970-01-01
      • 1970-01-01
      • 2019-11-09
      • 2017-11-07
      • 2019-03-16
      • 2014-04-16
      相关资源
      最近更新 更多