欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

WKWebView使用经验汇总

程序员文章站 2022-07-03 15:14:30
...

1、获取加载进度:

使用KOV来观察

[webView addObserver:selfforKeyPath:@"estimatedProgress"options:NSKeyValueObservingOptionNew context:NULL];

2、获取Title

[webView addObserver:selfforKeyPath:@"title"options:NSKeyValueObservingOptionNew context:NULL];

然后重写KVO的实现方法

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if (object == webView) {
        if ([keyPath isEqualToString:@"estimatedProgress"]) {
            //加载进度
            [progressView setAlpha:1.0f];
            [progressView setProgress:webView.estimatedProgress animated:YES];
            if(webView.estimatedProgress >= 1.0f) {
                [UIView animateWithDuration:0.3 delay:0.3 options:UIViewAnimationOptionCurveEaseOut animations:^{
                    [progressView setAlpha:0.0f];
                } completion:^(BOOL finished) {
                    [progressView setProgress:0.0f animated:NO];
                }];
            }
        } else if ([keyPath isEqualToString:@"title"]) {
            //页面标题
            self.title = webView.title;
        }
    } else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

3、返回某个历史页面

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    if (navigationAction.navigationType==WKNavigationTypeBackForward) {                  //判断是返回类型
        if (webView.backForwardList.backList.count>0) {                                  //得到栈里面的list
            WKBackForwardListItem * item = webView.backForwardList.currentItem;          //得到现在加载的list
            for (WKBackForwardListItem * backItem inwebView.backForwardList.backList) { //循环遍历,得到你想退出到
                //添加判断条件
                [webView goToBackForwardListItem:[webView.backForwardList.backListfirstObject]];
            }
        }
    }
    //允许跳转
    decisionHandler(WKNavigationActionPolicyAllow);
}

4、清除缓存

- (void)deleteWebCache {
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 9.0) {
        //系统版本高于iOS9
        NSSet *websiteDataTypes
        = [NSSet setWithArray:@[
                                WKWebsiteDataTypeDiskCache,
                                WKWebsiteDataTypeMemoryCache,
                                ]];
        //// All kinds of data
        //NSSet *websiteDataTypes = [WKWebsiteDataStore allWebsiteDataTypes];
        //// Date from
        NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
        //// Execute
        [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
            // Done
        }];
    } else {
        //系统版本低于iOS9
        NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSString *cookiesFolderPath = [libraryPath stringByAppendingString:@"/Cookies"];
        NSError *errors;
        [[NSFileManager defaultManager] removeItemAtPath:cookiesFolderPath error:&errors];
    }
}