ios – WKWebView估计进度

ios – WKWebView估计进度,第1张

概述我正在尝试在WKWebView中实现估计进度,但似乎无法弄清楚.你能帮我吗? 这就是我所得到的: self.view = self.webView;NSURL *url = [NSURL URLWithString:stringWeb];NSURLRequest *request = [NSURLRequest requestWithURL:url];WKWebViewConfigura 我正在尝试在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];    }}
总结

以上是内存溢出为你收集整理的ios – WKWebView估计进度全部内容,希望文章能够帮你解决ios – WKWebView估计进度所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1112655.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存