iOS應用中URL地址如何重新導向-b

來源:互聯網
上載者:User

標籤:

就用一個很簡單的例子

http://www.google.comGoogle的首頁

 

都知道現在瀏覽器中開啟google.com的話事實上會變成http://www.google.com.hk

 

網址被重新導向了

 

如何在app中完成重新導向呢

 

使用NSURLConnetion類的NSURLConnectionDataDelegate委託

NSURLConnectionDataDelegate委託中的這個方法

 

  1. - (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response  

可以得到重新導向以後的URL

看代碼

 

  1. #import "ViewController.h"  
  2.   
  3. @interface ViewController ()  
  4.   
  5. @end  
  6.   
  7. @implementation ViewController  
  8.   
  9. - (void)viewDidLoad {  
  10.     [super viewDidLoad];  
  11.       
  12.     NSURL *url = [NSURL URLWithString:@"http://www.google.com"];  
  13.     NSURLRequest *request = [NSURLRequest requestWithURL:url];  
  14.     NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];  
  15.       
  16.     if (!connection) {  
  17.         NSLog(@"FAIL");  
  18.     }  
  19. }  
  20.   
  21. - (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response {  
  22.     NSLog(@"================================================");  
  23.     NSLog(@"will send request\n%@", [request URL]);  
  24.     NSLog(@"redirect response\n%@", [response URL]);  
  25.       
  26.     return request;  
  27. }  
  28.   
  29. @end  


以上省略了部分無關代碼

 

解釋一下代碼

在viewDidLoad方法中進行了一次串連 正是Google的首頁

 

再看看這個

 


  1. - (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response  

 

 

這個方法在請求將要被發送出去之前會調用

傳回值是一個NSURLRequest就是那個真正將要被發送的請求

第二個參數request就是被重新導向處理過後的請求 在這裡就可以拿到需要的URL

第三個參數response是一個將要觸發重新導向的請求

 

這裡把request跟response中的URL打出來看一下

並直接返回request

 

運行看看結果

 

 

  1. ====will send request====  
  2. http://www.google.com/  
  3. ====redirect response====  
  4. (null)  
  5.   
  6.   
  7. ====will send request====  
  8. http://www.google.com.hk/url?sa=p&hl=zh-CN&pref=hkredirect&pval=yes&q=http://www.google.com.hk/&ust=1347589441099727&usg=AFQjCNEsBYcrlh_jqpBWRwsc0NpBj2_lFg  
  9. ====redirect response====  
  10. http://www.google.com/  
  11.   
  12.   
  13. ====will send request====  
  14. http://www.google.com.hk/  
  15. ====redirect response====  
  16. http://www.google.com.hk/url?sa=p&hl=zh-CN&pref=hkredirect&pval=yes&q=http://www.google.com.hk/&ust=1347589441099727&usg=AFQjCNEsBYcrlh_jqpBWRwsc0NpBj2_lFg  


可以看出進行了兩次重新導向

 

第一次

 


  1. ====will send request====  
  2. http://www.google.com/  
  3. ====redirect response====  
  4. (null)  

 

由於是第一次調用 沒有進行重新導向處理 所以redirect response是null

而想要被發送的請求就是www.google.com

 

第二次

 


  1. ====will send request====  
  2. http://www.google.com.hk/url?sa=p&hl=zh-CN&pref=hkredirect&pval=yes&q=http://www.google.com.hk/&ust=1347589441099727&usg=AFQjCNEsBYcrlh_jqpBWRwsc0NpBj2_lFg  
  3. ====redirect response====  
  4. http://www.google.com/  

 這個時候redirect response就不是null了 就是第一次中的那個request 也就是說這一次的重新導向是由www.google.com這個URL引發的

 

而重新導向的結果就是http://www.google.com.hk/url?sa=p&hl=zh-CN&pref=hkredirect&pval=yes&q=http://www.google.com.hk/&ust=1347589441099727&usg=AFQjCNEsBYcrlh_jqpBWRwsc0NpBj2_lFg

 

第三次

 

[html] view plaincopy 
  1. ====will send request====  
  2. http://www.google.com.hk/  
  3. ====redirect response====  
  4. http://www.google.com.hk/url?sa=p&hl=zh-CN&pref=hkredirect&pval=yes&q=http://www.google.com.hk/&ust=1347589441099727&usg=AFQjCNEsBYcrlh_jqpBWRwsc0NpBj2_lFg  

當然這個http://www.google.com.hk/url?sa=p&hl=zh-CN&pref=hkredirect&pval=yes&q=http://www.google.com.hk/&ust=1347589441099727&usg=AFQjCNEsBYcrlh_jqpBWRwsc0NpBj2_lFg

 

這個網址不是那個熟悉的網址

因為還要一次重新導向

這次就跟上面一樣了 那個很長的URL這次出現在了redirect response裡 觸發了這次重新導向

結果可以看到

就是那個熟悉的www.google.com.hk

 

當獲得想要的URL以後可以調用[connection cancel];方法來取消串連

並返回nil就好了

iOS應用中URL地址如何重新導向-b

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.