美文网首页
iOS WebView重定向新开界面问题

iOS WebView重定向新开界面问题

作者: Evans_Xiao | 来源:发表于2018-05-04 10:45 被阅读714次

    今早进入公司打开App出现新的问题----用H5加载的页面出现了需要登录验证.
    经过上网查证才知道是WebView页面重定向的导致.

    介绍一下导致的原因:因为公司网络需要认证登录.
    =================直接上图,贴代码=================

    1、加载请求的方法---

    - (void)loadData
    {
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",[DefaultsUserInfo shared].strMgrServeUrl,URL_HELP_FEEED]];
        NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url
                                                      cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                  timeoutInterval:10];
    
        [self.webView loadRequest:request];
    
        NSURLSession *session = [NSURLSession sharedSession];
        NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { }];
    
        //用于解决网络超时的问题
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(request.timeoutInterval * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [task cancel];
        });
    }
    

    2、WebView常用的代理的方法

    #pragma mark - UIWebViewDelegate
    - (void)webViewDidStartLoad:(UIWebView *)webView;
    - (void)webViewDidFinishLoad:(UIWebView *)webView;
    - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;
    

    以上三个方法的具体实现根据需求而定,这里不一一介绍了(懒得废话)

    3、最重要却又容易忽略的方法

    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
    

    结合webViewDidFinishLoad:这里我贴出我的具体实现:

    - (void)webViewDidFinishLoad:(UIWebView *)webView
    {
        if (self.webViewDidFinishLoad)
        {
            self.webViewDidFinishLoad(YES);
        }
    
        if ([self.webView.request.URL.absoluteString containsString:@"about:blank"])
        {
            NSError *err;
            [self webView:self.webView didFailLoadWithError:err];
        }
    }
    
    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
    {
        if ([request.URL.absoluteString containsString:[DefaultsUserInfo shared].strMgrServeUrl] ||[request.URL.absoluteString containsString:@"about:blank"])
        {
            return YES;
        }else {
            NSURL *url = [NSURL URLWithString:@"about:blank"];
            NSURLRequest *requestRedirect = [[NSURLRequest alloc] initWithURL:url
                                                           cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                       timeoutInterval:10];
            [self.webView loadRequest:requestRedirect];
            return NO;
        }
    }
    

    具体介绍:
    可根据request.URL.absoluteString获取到当前requestURL是否是我们实际App中请求的地址,若是则可继续返回Yes,继续等待服务器响应;否则,利用空白页代替之前请求(之所以用空白页是因为其他不确定因素会详情其他元素,影响App可观性).

    =================谢谢观赏~~~=================

    空白页打开方式,可百度得知.
    about:blank是打开浏览器空白页的命令。


    about:blank

    相关文章

      网友评论

          本文标题:iOS WebView重定向新开界面问题

          本文链接:https://www.haomeiwen.com/subject/zwzrrftx.html