代码之家  ›  专栏  ›  技术社区  ›  Peter

在ios uiwebview objective-c中检查布尔值是否为yes或no时出错

  •  -1
  • Peter  · 技术社区  · 7 年前

    我正在尝试检查在IOS UIWebView中加载的URL是否有错误,如果 200 我要设置 isRequestWebOk 到 YES 否则我将使用默认值 NO ,除了 isRequestWebOk 仍然返回 不 即使状态码是200,我也会设置它 if(200){isRequestWebOk = YES;} 。 请任何人帮我一下,我不知道为什么工作不正常。

    -(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType{
        static BOOL isRequestWeb = YES;
        static BOOL isRequestWebOk = NO;
        NSString *embedHTML = @"<html><head></head><body style='color:#000;'><p>Error</p></body></html>";
    
        if (isRequestWeb) {
            //NSHTTPURLResponse *response = nil;
            //NSData *data = [NSURLConnection sendSynchronousRequest:inRequest returningResponse:&response error:nil];
            //if (response.statusCode == 404) {}
    
            [[[NSURLSession sharedSession] dataTaskWithRequest:inRequest completionHandler:^(NSData * data, NSURLResponse * response, NSError * error) {
                NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
                NSInteger statusCode = httpResponse.statusCode;
                if (statusCode >= 200 && statusCode < 300){
                    /*I think this do set but i can't access it outside the completionHandler but i really need to check it outside as i cannot do many other things inside here*/
                    isRequestWebOk = YES;
                    NSLog(@"GOOD");
                }else if (statusCode == 404) {
                    NSLog(@"code for 404");
                }else if (statusCode == 403) {
                    NSLog(@"code for 403");
                }else if (statusCode == 500) {
                    NSLog(@"code for 500");
                }else{
                    NSLog(@"code for ALL");
                    isRequestWebOk = YES;
                }
                NSLog(@"code is what: %ld", (long)statusCode);
            }] resume];
            isRequestWeb = NO;
    
            /*This is where am checking for isRequestWebOk if true or not but it always return false*/
            if(isRequestWebOk){
                NSLog(@"Request is okay 200");
                return YES;
            }else{
                NSLog(@"Bad Request b4004");
                inWeb.userInteractionEnabled = NO;
                inWeb.opaque = NO;
                inWeb.backgroundColor = [UIColor clearColor];
                [inWeb loadHTMLString: embedHTML baseURL: nil];
                return NO;
            }
        }
        return YES;
    }
    

    我认为这个确实设置了,但我不能在completionHandler之外访问它,但我真的需要在外面检查它,因为我不能在里面做很多其他事情

    1 回复  |  直到 7 年前
        1
  •  0
  •   Mo Abdul-Hameed Martheli    7 年前

    您需要移动此代码块:

    if(isRequestWebOk){
        NSLog(@"Request is okay 200");
        return YES;
    }else{
        NSLog(@"Bad Request b4004");
        inWeb.userInteractionEnabled = NO;
        inWeb.opaque = NO;
        inWeb.backgroundColor = [UIColor clearColor];
        [inWeb loadHTMLString: embedHTML baseURL: nil];
        return NO;
    }
    

    到内部 completionHandler 。

    您在此处检查时,如果 isRequestWebOk 是 YES 或 NO 请求可能仍在进行中。