简体   繁体   English

我如何保持当前页面直到下一页完全加载(iOS UIWebview)

[英]How can i keep current page until the next page is fully loaded( ios UIWebview)

I have an app based on UIWebview , When a user clicks on a URL, the default behavior is to immediately show a blank page and stay waiting until the page is loaded 我有一个基于UIWebview的应用程序,当用户单击URL时,默认行为是立即显示空白页面并保持等待状态,直到页面加载UIWebview

Is that possible to keep the current page until the next page is loaded? 是否可以保留当前页面直到下一页加载?

Thanks for advance. 感谢前进。

I'm curious as to the necessity for this funtionality and I'd probably agree with Chandra Vaghasiya 's comment . 我对这种功能的必要性感到好奇,我也许会同意钱德拉•瓦加西亚Chandra Vaghasiya )的评论

Having said that this is possible. 话虽如此,这是可能的。

  1. Make your view controller the delegate of the UIWebView . 使视图控制器成为UIWebView的委托。
  2. In the webViewDidStartLoad: method, take a snapshot of the web view and add the resulting view above the web view in the view hierarchy. webViewDidStartLoad:方法中,拍摄Web视图的快照,并将结果视图添加到视图层次结构中Web视图上方。
  3. In the webViewDidFinishLoad: and webView:didFailLoadWithError: methods simply hide the snapshotted view. webViewDidFinishLoad:webView:didFailLoadWithError:方法中,仅隐藏快照视图。

Example code: 示例代码:

@interface ViewController () <UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (nonatomic, strong) UIView *snapshotView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    NSURL *url = [NSURL URLWithString:@"https://google.com"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    self.snapshotView = [self.webView snapshotViewAfterScreenUpdates:NO];

    self.snapshotView.frame = self.webView.frame;

    [self.view insertSubview:self.snapshotView
                aboveSubview:self.webView];

    self.snapshotView.hidden = NO;

    // THIS IS JUST FOR DEMONSTRATION PURPOSES SO
    // YOU CAN SEE THE LOADING HAPPENING BEHIND
    // THE SNAPSHOT VIEW
    self.snapshotView.alpha = 0.8;

    NSLog(@"Display snapshot");
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    self.snapshotView.hidden = YES;
    NSLog(@"Hide snapshot");
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    self.snapshotView.hidden = YES;
    NSLog(@"Hide snapshot due to error");
}

@end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM