@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
相关推荐
15 Oct 12 UHDD UIDE again detect A: and B: diskettes from BIOS data NOT from "Int 13h" calls that FAIL with an LS 120 drive 2 Aug 12 UHDD "disk only" caching driver added ...
What’s New in Python What’s New In Python 3.6 Summary – Release highlights New Features PEP 498: Formatted string literals PEP 526: Syntax for variable annotations PEP 515: Underscores in ...
python教程学习
tdac034.pdf
veclibm111111111111
nssm工具,进行2次解压后即可
视讯镜头优化,操作不走
【数据结构】的资源,包括书籍、在线课程和网站
北京大学-DeepSeek系列-提示词工程和落地场景.pdf.png
【财信国际经济研究院-2025研报】2025年2月CPI和PPI数据点评:通胀低于预期,内需亟待提振.pdf
【毕业设计】java-springboot+vue精品在线试题库系统源码(完整前后端+mysql+说明文档+LunW).zip
python安装-25.求 a 的 b 次方——有点不好意思哈.py
代码实现了自适应IMM(交互式多模型)算法,专注于对目标状态进行估计,并结合了匀速(CV)和匀加速(CA)运动模型。它使用自适应观测噪声估计和扩展卡尔曼滤波器(EKF)作为滤波主体,并在最后提供误差统计特性输出。
中产品工业物联网联网的基础功能,是企业开启数字化转型的入门之选。它能帮助企业快速搭建起基础的设备数据采集与监控体系,以较低成本实
北京大学DeepSeek系列-DeepSeek与AIGC应用.pdf.png
【毕业设计-java】springboot-vue基于多维分类的知识管理系统实现源码(完整前后端+mysql+说明文档+LunW).zip
## 一、数据介绍 本数据参考C刊《管理评论》佟岩(2024)老师的做法,使用各年度省级政府工作报告中“减碳”关键词的词频总数来测度地方政府对“减碳”的重视程度,“减碳”关键词包括二氧化碳、低碳、减排、节能、能耗、环境保护(环保)、生态、绿色。 本数据包含:原始数据、参考文献、代码do文件、最终结果。 ## 二、参考文献: 佟岩,李鑫,徐国铨.企业集团碳减排压力与债务分布-来自高耗能上市公司的经验证据[J].管理评论,2024,36(02):210-221.DOI:10.14120/j.cnki.cn11-5057/f.2024.02.014 ## 三、相关数据:城市、城市编码、年份、减碳重视程度词频。
python教程学习
jfinal-undertow 用于开发、部署由 jfinal 开发的 web 项目
python学习一些项目和资源