重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章主要为大家展示了“iOS如何拦截重定向302跳转”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“iOS如何拦截重定向302跳转”这篇文章吧。
目前成都创新互联已为近千家的企业提供了网站建设、域名、网页空间、网站托管维护、企业网站设计、东胜网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
一:前言
公司同事在做项目的时候遇到一个302地址跳转的问题,具体需求如下:
1、公司是做WIFI覆盖的,需要在下载APP后才能注册登录以后,自动连接到外网
2、苹果底层不允许我们码农进行WIFI的切换,所以公司考虑使用302重定向来实现,我通过2种方法来实现,NSURLConnetion和NSURLSession
二:NSURLConnetion方法
大概的思路:使用NSURLConnetion类的NSURLConnectionDataDelegate中的代理方法
- (nullable NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(nullable NSURLResponse *)response
新建一个DEMO项目的话,记得把App Transport Security Settings Allow Arbitrary Loads设置为YES,否则无法请求网络
- (void)viewDidLoad { [super viewDidLoad]; NSURL *url = [NSURL URLWithString:@"http://www.google.com"]; NSMutableURLRequest *quest = [NSMutableURLRequest requestWithURL:url]; quest.HTTPMethod = @"GET"; NSURLConnection *connect = [NSURLConnection connectionWithRequest:quest delegate:self]; [connect start]; } - (nullable NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(nullable NSURLResponse *)response{ NSHTTPURLResponse *urlResponse = (NSHTTPURLResponse *)response; NSLog(@"%ld",urlResponse.statusCode); NSLog(@"%@",urlResponse.allHeaderFields); NSDictionary *dic = urlResponse.allHeaderFields; NSLog(@"%@",dic[@"Location"]); return request; }
request 是你要发送的请求 urlResponse.statusCode 则是请求的状态码,302则是重定向 。urlResponse.allHeaderFields中保存着http的头信息:
{ Connection = close; "Content-Type" = "text/html"; Date = "Mon, 30 May 2016 04:00:49 GMT"; Location = "http://118.244.233.137:6001/login/?gw_address=192.168.17.1&gw_port=2060&gw_id=ctzx_11&ip=192.168.17.24&mac=54:72:4f:30:c6:10&url=http%3A%2F%2Fwww.google.com%2F"; Server = "Hughes Technologies Embedded Server"; }
通过urlResponse.allHeaderFields[@"Location"]即可拿到302跳转的url地址.如果需要拦截掉这个url地址跳转,则可在代理方法中自定义一个request
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPerformHTTPRedirection:(NSHTTPURLResponse *)response newRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLRequest * __nullable))completionHandler{ //block为nil,则直接拦截掉302的url地址,防止自动跳转 completionHandler(nil); }
则直接拦截掉302的url地址,防止自动跳转 completionHandler(nil);}
三:NSURLSession方法
- (void)viewDidLoad { [super viewDidLoad]; NSURL *url = [NSURL URLWithString:@"http://www.google.com"]; NSMutableURLRequest *quest = [NSMutableURLRequest requestWithURL:url]; quest.HTTPMethod = @"GET"; NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration]; config.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData; NSURLSession *urlSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue currentQueue]]; NSURLSessionDataTask *task = [urlSession dataTaskWithRequest:quest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { NSHTTPURLResponse *urlResponse = (NSHTTPURLResponse *)response; NSLog(@"%ld",urlResponse.statusCode); NSLog(@"%@",urlResponse.allHeaderFields); NSDictionary *dic = urlResponse.allHeaderFields; NSLog(@"%@",dic[@"Location"]); }]; [task resume]; } - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPerformHTTPRedirection:(NSHTTPURLResponse *)response newRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLRequest * __nullable))completionHandler{ completionHandler(nil); }
以上是“iOS如何拦截重定向302跳转”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!