iOS 攔截重定向302跳轉(zhuǎn)的方法詳解
一:前言
公司同事在做項目的時候遇到一個302地址跳轉(zhuǎn)的問題,具體需求如下:
1、公司是做WIFI覆蓋的,需要在下載APP后才能注冊登錄以后,自動連接到外網(wǎng)
2、蘋果底層不允許我們碼農(nóng)進行WIFI的切換,所以公司考慮使用302重定向來實現(xiàn),我通過2種方法來實現(xiàn),NSURLConnetion和NSURLSession
二:NSURLConnetion方法
大概的思路:使用NSURLConnetion類的NSURLConnectionDataDelegate中的代理方法
- (nullable NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(nullable NSURLResponse *)response
都是碼農(nóng),直接上代碼吧
新建一個DEMO項目的話,記得把App Transport Security Settings Allow Arbitrary Loads設置為YES,否則無法請求網(wǎng)絡
- (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 是你要發(fā)送的請求 urlResponse.statusCode 則是請求的狀態(tài)碼,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跳轉(zhuǎn)的url地址.如果需要攔截掉這個url地址跳轉(zhuǎn),則可在代理方法中自定義一個request
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
willPerformHTTPRedirection:(NSHTTPURLResponse *)response
newRequest:(NSURLRequest *)request
completionHandler:(void (^)(NSURLRequest * __nullable))completionHandler{
//block為nil,則直接攔截掉302的url地址,防止自動跳轉(zhuǎn)
completionHandler(nil);
}
則直接攔截掉302的url地址,防止自動跳轉(zhuǎn) 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);
}
使用方法和第一種方法一樣
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
iOS使用WKWebView加載HTML5不顯示屏幕寬度的問題解決
這篇文章主要介紹了iOS使用WKWebView加載HTML5不顯示屏幕寬度的問題解決,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12
IOS AFNetworking的Post失敗及requestSerializer的正確使用
這篇文章主要介紹了IOS AFNetworking的Post失敗及requestSerializer的正確使用的相關資料,需要的朋友可以參考下2017-05-05
iOS利用UIBezierPath + CAAnimation實現(xiàn)路徑動畫效果
在iOS開發(fā)中,制作動畫效果是最讓開發(fā)者享受的環(huán)節(jié)之一,這篇文章主要給大家介紹了關于iOS利用UIBezierPath + CAAnimation實現(xiàn)路徑動畫效果的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧。2017-10-10

