@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 深度解析** 在现代Web开发中,数据抓取和网络爬虫已经成为获取大量信息的重要手段。然而,对于网站开发者来说,有时需要识别并处理这些自动访问的爬虫,以保护网站资源、...
DDR5 Serial Presence Detect (SPD)是DDR5内存模块中一个关键组件,它负责存储关于内存模块配置的重要信息。JESD400-5是JEDEC固态技术协会制定的一项标准,详细定义了DDR5 SPD的内容和格式。这个标准的版本号为0.89...
tb-combatting-ata-detect-cn.pdf
在当今的人工智能领域中,深度学习技术正扮演着核心角色。尤其是在图像识别和处理方面,卷积神经网络(Convolutional Neural Network, CNN)已经成为一种非常强大的工具。本篇教程详细介绍了如何使用卷积神经网络来...
【Laravel开发-laravel-mobile-detect】是一个专为Laravel框架设计的扩展包,用于实现对用户设备的即时移动检测。在现代Web开发中,针对不同设备(如桌面、手机和平板)提供优化的用户体验至关重要。`laravel-mobile...
在上述提到的“rotated-box-object-detect-main”文件中,我们可以合理推断这是一个包含核心代码的主文件夹,可能包含模型加载、预处理图像数据、执行推理以及后处理推理结果等功能。在该文件夹中,开发者可能会定义...
像素边缘检测和亚像素边缘检测是计算机视觉领域中图像处理的重要技术,主要应用于图像...通过研究"sub-pixel-edge-detect-master"项目,不仅可以学习到边缘检测的基本原理,还能了解到如何在实际项目中应用这些技术。
在压缩包文件rn-host-detect-master中,可能包含以下内容: 1. `README.md`:项目介绍和使用指南,详细说明了如何安装和使用rn-host-detect库。 2. `index.js`或`src`目录下的源码文件:包含rn-host-detect的主要...
在“use-yolov8-to-detect-trash-master.zip”压缩包中,可能包含了以下内容: 1. 训练代码:使用Python编写的脚本,用于设置YOLOv8模型参数、加载数据集、训练模型等。 2. 数据集:包含标注的水体图像,每个图像都...
【80-P8536-1-A-QUALCOMM-CHARGER-DETECT-ISSUES-DEBUG】指南是高通(Qualcomm)公司为解决其电源管理集成电路(PMIC)在充电检测过程中遇到的问题而提供的内部调试文档。该文档详细阐述了如何处理与高通充电器检测...
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 ...
总的来说,"javascript-detect-element-resize" 提供了一个强大的工具,使得前端开发者能够轻松处理元素尺寸变化带来的挑战。通过掌握这个库,你可以创建更智能、更动态的前端应用,提供更好的用户体验。
在"es-feature-detect-master"这个压缩包中,我们可以找到这个库的源代码和其他相关资源。通过阅读和理解源码,开发者可以深入学习如何设计这样的检测库,这对于提升自身技能和理解JavaScript的底层机制大有裨益。 ...
"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 ...
FPGA SATA+RAID0 xilinx vivado z7 k7存储 SATA Features - Detect OOB and COMWAKE - Detect the K28.5 comma character and provide a 16 bit parallel output ...- Report transmission status and error to Transpo
含有以下内容:train/|----- pose 精简的2d关键点训练代码eval/|----- keypoint_detect 2d关键点模型评估、预测代码|----- object_detect 目标检测评估、预测代码|----- objectdetect_pipline 可拓展的目标检测流程...
1、YOLOv8文本中表格检测,检测模型已经训练好,包含以及PR曲线,loss曲线等等。类别名为table;用于识别各种文档、本文中的表格区域; 2、并附有数据集,使用lableimg软件标注软件标注好的文本表格检测数据,图片...
为了维护网络环境的健康,"Machine-Learning-Approach-to-Detect-Cyber-Trolls" 项目旨在利用机器学习技术来自动识别并阻止这类行为。 该项目基于 Jupyter Notebook 实现,这是一个广泛用于数据分析、机器学习和...