ios – WKWebView估计进度

前端之家收集整理的这篇文章主要介绍了ios – WKWebView估计进度前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试在WKWebView中实现估计进度,但似乎无法弄清楚.你能帮我吗?

这就是我所得到的:

self.view = self.webView;

NSURL *url = [NSURL URLWithString:stringWeb];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];

self.webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:configuration];

[self.webView loadRequest:request];

我看到这个答案有一点,但这是一个微调:UIWebView with Progress Bar

苹果公司记录了一些估计的进度(我假设它的导航栏下方的蓝色条,显示了Safari中的进度),但是我一般不会看到如何实现:https://developer.apple.com/library/ios/documentation/WebKit/Reference/WKWebView_Ref/#//apple_ref/occ/instp/WKWebView/estimatedProgress

所以我被困在这里任何帮助将不胜感激,谢谢!

更新:这是我现在所拥有的.因为看起来像我的进度视图和WKWebView正在加载两次,因为崩溃,我不知道为什么会这样.得到观察者需要删除错误.这是我的代码,

ViewController.h

@interface WebPageViewController : UIViewController <UIWebViewDelegate>
@property (strong,nonatomic) NSString *stringMobile;
@property (strong,nonatomic) NSString *stringWeb;
@property (strong,nonatomic) IBOutlet UIView *view;
@property (nonatomic,strong) WKWebView *webView;
@property (nonatomic) UIProgressView *progressView;

ViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:self.webView];

    [self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:NULL];

    self.progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
    self.progressView.center = self.view.center;
    [self.view addSubview:self.progressView];

    NSURLRequest *URLRequest = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:stringWeb]];
    [self.webView loadRequest:URLRequest];


}

- (void)dealloc {
    [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];

    // if you have set either WKWebView delegate also set these to nil here
    [self.webView setNavigationDelegate:nil];
    [self.webView setUIDelegate:nil];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqualToString:@"estimatedProgress"] && object == self.webView) {
        [self.progressView setAlpha:1.0f];
        [self.progressView setProgress:self.webView.estimatedProgress animated:YES];

        if(self.webView.estimatedProgress >= 1.0f) {
            [UIView animateWithDuration:0.3 delay:0.3 options:UIViewAnimationOptionCurveEaSEOut animations:^{
                [self.progressView setAlpha:0.0f];
            } completion:^(BOOL finished) {
                [self.progressView setProgress:0.0f animated:NO];
            }];
        }
    }
    else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

更新:使用CocoaPods这是我所拥有的,但它显示两个视图而不是一个webview

- (void)viewDidLoad {
    [super viewDidLoad];
    NSURL *myURL = [NSURL URLWithString: [self.url stringByAddingPercentEscapesUsingEncoding:
                                          NSUTF8StringEncoding]];
    NSURLRequest *request = [NSURLRequest requestWithURL:myURL];
    //[self.webView loadRequest:request];

    // KIN
    // Deleted UIWebView in Storyboard
    KINWebBrowserViewController *webBrowser = [[KINWebBrowserViewController alloc] init];
    [self.navigationController pushViewController:webBrowser animated:YES];
    [webBrowser loadURL:myURL];
}

解决方法

在GitHub上查看 KINWebBrowser,查看下面解决方案的全面实现.

如果您仔细查看WKWebView的估计Progress属性的文档,您链接到的文档将会显示

The WKWebView class is key-value observing (KVO) compliant for this property.

这意味着您可以在estimateProgress属性上设置键值观察以观察其值的更改.从observeValueForKeyPath方法可以更新您的UI.

可可的KVO设计模式是相当凌乱的.看看这个优秀的NSHipster有关Key Value Observing最佳实践的文章.

以下是WKWebView上估计的进度的KVO实现:

从您的UIViewController,设置您的WKWebView并添加自己作为estimatedProgress的观察者

self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:self.webView];

    [self.webView addObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress)) options:NSKeyValueObservingOptionNew context:NULL];

在同一个UIViewController中,设置observeValueForKeyPath方法来过滤掉webView的estimateProgress属性.然后,您可以直接访问estimateProgress值,并相应地更新您的UI.

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqualToString:NSStringFromSelector(@selector(estimatedProgress))] && object == self.webView) {
        NSLog(@"%f",self.webView.estimatedProgress);
        // estimatedProgress is a value from 0.0 to 1.0
        // Update your UI here accordingly
    }
    else {
        // Make sure to call the superclass's implementation in the else block in case it is also implementing KVO
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

确保在UIViewController的dealloc方法中从UIViewController中删除KVO.如果观察者尚未添加,请检查isViewLoaded是否避免崩溃是非常重要的.

- (void)dealloc {

    if ([self isViewLoaded]) {
        [self.wkWebView removeObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress))];
    }

    // if you have set either WKWebView delegate also set these to nil here
    [self.wkWebView setNavigationDelegate:nil];
    [self.wkWebView setUIDelegate:nil];
}

要看这个在一些大文件的行动,加载这个甜蜜的星系的巨大的图像文件. (这个文件是35MB,确保你在WiFi上!)

NSURLRequest *URLRequest = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.spacetelescope.org/static/archives/images/large/opo0328a.jpg"]];
    [self.webView loadRequest:URLRequest];

如果您正在使用UIProgressView,您可以使用此代码实现一个诸如淡出效果的Safari:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqualToString:NSStringFromSelector(@selector(estimatedProgress))] && object == self.wkWebView) {
        [self.progressView setAlpha:1.0f];
        [self.progressView setProgress:self.wkWebView.estimatedProgress animated:YES];

        if(self.wkWebView.estimatedProgress >= 1.0f) {
            [UIView animateWithDuration:0.3 delay:0.3 options:UIViewAnimationOptionCurveEaSEOut animations:^{
                [self.progressView setAlpha:0.0f];
            } completion:^(BOOL finished) {
                [self.progressView setProgress:0.0f animated:NO];
            }];
        }
    }
    else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}
原文链接:https://www.f2er.com/iOS/337145.html

猜你在找的iOS相关文章