WebView重定向新开界面问题-b

时间:2021-02-24 21:04:22

首先介绍下这个问题,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://*.com上找到的。附上原连接http://*.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://*.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];
}

完美解决。