標籤:
首先介紹下這個問題,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