我正在尝试检查在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之外访问它,但我真的需要在外面检查它,因为我不能在里面做很多其他事情