iOS WebView重定向新開界面問題

這個問題困擾我好久了。終於搞好了。不容易啊。
首先介紹下這個問題,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];
}

完美解決。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章