美文网首页
点击UIWebView内的链接跳转新界面

点击UIWebView内的链接跳转新界面

作者: 黎先生_ | 来源:发表于2018-06-27 09:12 被阅读0次

    项目问题:在打开的UIWebView中有新的链接,点击打开后会出现空白现象。

    解决办法:在点击新的链接时重现跳转到一个新的界面

    声明两个属性:

    @property (nonatomic,assign) BOOL isLoad;
    @property (nonatomic,strong) NSString *loadedURL;
    

    首先实现UIWebView的代理

    webview.delegate = self;
    

    在UIWebView的代理方法中实现:

    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
        if (navigationType != UIWebViewNavigationTypeOther) {
            self.loadedURL = request.URL.absoluteString;
        }
        if (!_isLoad && [request.URL.absoluteString isEqualToString:self.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:self.loadedURL 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];
        }
        RootWebViewControl *webView = [[RootWebViewControl alloc]init];
        webView.url = newUrl;
        webView.isShowCloseBtn = YES;
        [self.navigationController pushViewController:webView animated:YES];
    }
    - (void)viewWillAppear:(BOOL)animated
    {
        _isLoad = NO;
        [super viewWillAppear:animated];
    }
    

    done!

    相关文章

      网友评论

          本文标题:点击UIWebView内的链接跳转新界面

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