WebView重新導向新開介面問題-b

來源:互聯網
上載者:User

標籤:

首先介紹下這個問題,iOS上WebView 如果想更貼近native,就要載入新URL的時候新開個介面,但是如果載入的連結有重新導向的話,就會在中間開一個空白的介面,這個好煩。然後就是解決這個問題,採用了很多辦法。重新導向的response的code是301,所以就向這個方向努力。首先在網上找到了一個方法。 原串連:http://blog.csdn.net/sheldongreen/article/details/7977802#

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

 

這是NSURLConnectionDataDelegate下的一個方法。 
用法是醬紫滴

- (void)viewDidLoad {      [super viewDidLoad];      NSURL *url = [NSURL URLWithString:@"http://www.google.com"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; } - (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response { NSLog(@"will send request\n%@", [request URL]); NSLog(@"redirect response\n%@", [response URL]); return request; } 

 

這種最終沒試過,但是運行起來是

====will send request====  http://www.google.com/  ====redirect response====  (null)  ====will send request====  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 ====redirect response==== http://www.google.com/ ====will send request==== http://www.google.com.hk/ ====redirect response==== 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 

 

但是這種不確定是重新導向還是介面內跳轉,而且新開介面不太好做(有興趣的可以試試)。 
pass掉

然後朋友介紹了第二種方法,在http://stackoverflow.com上找到的。附上原串連http://stackoverflow.com/questions/13505889/how-to-get-redirected-url。

-(void)webViewDidStartLoad:(UIWebView *)webView{    NSURL *url = [webView.request mainDocumentURL];    NSLog(@"The Redirected URL is: %@", url);}

 

依然是同上的問題。 pass掉。

終於,還是找到第三個方法。http://stackoverflow.com/questions/1061364/how-do-i-get-the-last-http-status-code-from-a-uiwebview 
這個的原理是它的原理是先請求 然後用webview載入

    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{    if (navigationType != UIWebViewNavigationTypeOther) {        self.loadedURL = request.URL.absoluteString; } if (!isLoad && [request.URL.absoluteString isEqualToString:loadedURL]) { [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { if (connectionError || ([response respondsToSelector:@selector(statusCode)] && [((NSHTTPURLResponse *)response) statusCode] != 200 && [((NSHTTPURLResponse *)response) statusCode] != 302)) { //Show error message [self showErrorMessage]; }else { isLoad = YES; [self showPage:absoluteString query:nil]; } }]; return NO; } isLoad = NO; return YES;}//新開介面-(void)showPage:(NSString *)url query:(NSString *)query{ NSString * newUrl = [NSString stringWithFormat:@"%@", url]; if (query) { newUrl = [NSString stringWithFormat:@"%@?%@", url,query]; } JFWebViewController *webView = [[JFWebViewController alloc]init]; webView.webURL = newUrl; [self.navigationController pushViewController:webView animated:YES];}- (void)viewWillAppear:(BOOL)animated{ _isLoad = NO; [super viewWillAppear:animated];}

完美解決。

WebView重新導向新開介面問題-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.