這篇文章主要為大家展示了“iOS如何攔截重定向302跳轉(zhuǎn)”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學習一下“iOS如何攔截重定向302跳轉(zhuǎn)”這篇文章吧。
目前成都創(chuàng)新互聯(lián)已為近千家的企業(yè)提供了網(wǎng)站建設、域名、網(wǎng)頁空間、網(wǎng)站托管維護、企業(yè)網(wǎng)站設計、東勝網(wǎng)站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
?一:前言
公司同事在做項目的時候遇到一個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
新建一個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); }
以上是“iOS如何攔截重定向302跳轉(zhuǎn)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!