您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“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跳轉”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。