@interface ViewController () <UIWebViewDelegate, NSURLConnectionDataDelegate> @property (nonatomic) BOOL validatedRequest; @property (nonatomic, strong) NSURL *originalUrl; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // since `shouldStartLoadWithRequest` only validates when a user clicks on a link, we'll bypass that // here and go right to the `NSURLConnection`, which will validate the request, and if good, it will // load the web view for us. self.originalUrl = [NSURL URLWithString:@"http://www.stackoverflow.com"]; NSURLRequest *request = [NSURLRequest requestWithURL:self.originalUrl]; [NSURLConnection connectionWithRequest:request delegate:self]; } #pragma mark - UIWebViewDelegate // you will see this called for 404 errors - (void)webViewDidFinishLoad:(UIWebView *)webView { self.validatedRequest = NO; // reset this for the next link the user clicks on } // you will not see this called for 404 errors - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { NSLog(@"%s error=%@", __FUNCTION__, error); } // this is where you could, intercept HTML requests and route them through // NSURLConnection, to see if the server responds successfully. - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { // we're only validating links we click on; if we validated that successfully, though, let's just go open it // nb: we're only validating links we click on because some sites initiate additional html requests of // their own, and don't want to get involved in mediating each and every server request; we're only // going to concern ourselves with those links the user clicks on. if (self.validatedRequest || navigationType != UIWebViewNavigationTypeLinkClicked) return YES; // if user clicked on a link and we haven't validated it yet, let's do so self.originalUrl = request.URL; [NSURLConnection connectionWithRequest:request delegate:self]; // and if we're validating, don't bother to have the web view load it yet ... // the `didReceiveResponse` will do that for us once the connection has been validated return NO; } #pragma mark - NSURLConnectionDataDelegate method // This code inspired by http://www.ardalahmet.com/2011/08/18/how-to-detect-and-handle-http-status-codes-in-uiwebviews/ // Given that some ISPs do redirects that one might otherwise prefer to see handled as errors, I'm also checking // to see if the original URL's host matches the response's URL. This logic may be too restrictive (some valid redirects // will be rejected, such as www.adobephotoshop.com which redirects you to www.adobe.com), but does capture the ISP // redirect problem I am concerned about. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; NSString *originalUrlHostName = self.originalUrl.host; NSString *responseUrlHostName = response.URL.host; NSRange originalInResponse = [responseUrlHostName rangeOfString:originalUrlHostName]; // handle where we went to "apple.com" and got redirected to "www.apple.com" NSRange responseInOriginal = [originalUrlHostName rangeOfString:responseUrlHostName]; // handle where we went to "www.stackoverflow.com" and got redirected to "stackoverflow.com" if (originalInResponse.location == NSNotFound && responseInOriginal.location == NSNotFound) { NSLog(@"%s you were redirected from %@ to %@", __FUNCTION__, self.originalUrl.absoluteString, response.URL.absoluteString); } else if (httpResponse.statusCode < 200 || httpResponse.statusCode >= 300) { NSLog(@"%s request to %@ failed with statusCode=%d", __FUNCTION__, response.URL.absoluteString, httpResponse.statusCode); } else { [connection cancel]; self.validatedRequest = YES; [self.webView loadRequest:connection.originalRequest]; return; } [connection cancel]; } @end
相关推荐
Laravel开发-laravel-crawler-detect 通过用户代理检测网络爬虫的Laravel包
yolov8系列-- Using YOLOv8 to detect face parts
Use YOLOv8 to detect trash使用 YOLOv8 实现在水体图像中检测垃圾use-yolov8-to-detect-trash-master.zip
利用卷积神经网络进行深度学习,识别人脸特征点,从头到尾细致讲解。
Laravel开发-laravel-mobile-detect 即时移动检测直接从刀片模板内访问。
前端项目-javascript-detect-element-resize,基于事件的跨浏览器元素大小调整检测
python detect.py --weights best.pt --source 0 python detect.py --weights best.pt --source 0 python detect.py --weights best.pt --source 0 python detect.py --weights best.pt --source 0 python ...
Chapter 1, Introduction, discusses the concept of shellcode, buffer...Chapter 12, Detection and Prevention, discusses the techniques and algorithms you need to detect and prevent buffer overflow attacks.
藏经阁-Deep Learning for Fraud Detect.pdf
fall-detect-track项目的模型权重
Detect similarities and differences in data with clustering Work with Jupyter Notebook to produce publication-ready figures to be included in reports In Detail Data analysis is the process of ...
80_P8536_1_A_QUALCOMM_CHARGER_DETECT_ISSUES_DEBUG_
基于上下文的停车位检测(IEEE Access)的Tensorflow实现此实现基于准备数据集(PIL暂存区) 此代码一开始只能运行一次。 下载火车数据集解压缩到$ your_data_path / train文件夹下载测试数据集解压缩到$ your_data_...
前端项目-js-objectdetect,浏览器中的计算机视觉-javascript实时对象检测
including Penetration Tester's Open Source Toolkit (ISBN: 1-5974490210), Stealing the Network: How to Own an Identity (ISBN: 1597490067), InfoSec Career Hacking (ISBN: 1597490113), and OS X for ...
前端开源库-es-feature-detectES功能检测,简单的ECMAScript功能检测模块
前端开源库-rn-host-detectRN主机检测,在Android仿真器上正确获取本地主机,以实现本地响应
2、在yolov5-6.1的yolov5n.yaml文件中添加了一个小目标检测层。 同理, yolov5s.yaml、yolov5m.yaml、yolov5l.yaml、yolov5x.yaml也可以像yolov5n.yaml这样添加。 3、训练、测试、预测命令与官方版本一致。
JESD400-5_DDR5 Serial Presence Detect(SPD) Contents_Rev 0.89 (2),官网下的,还需要注册啥的,每天给我发邮件。。。。
cordova-screenshot cordova截屏插件 document.addEventListener("deviceready", init, false); function init() { document.querySelector(... //should be path/to/myScreenshot.jpg } },'jpg',50,'Share'); }}