<h1>一:前言</h1>
公司同事在做项目的时候遇到一个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);
}
使用方法和第一种方法一样
Demo下载
需要更多了解,请移步到iOS 拦截重定向302跳转的方法