我正在尝试Estimated Progress
在我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
Apple 记录了某种文件estimatedProgress
(我假设导航栏正下方的蓝色细条显示了 Safari 中的进度),但我一般不知道如何实现:https ://developer.apple.com /library/ios/documentation/WebKit/Reference/WKWebView_Ref/#//apple_ref/occ/instp/WKWebView/estimatedProgress
所以我被困在这里。任何帮助将不胜感激,谢谢!
更新:这就是我现在所拥有的。出现崩溃是因为我的 Progress View 和 WKWebView 似乎加载了两次,我不确定为什么会这样。收到需要删除观察者的错误。这是我的代码 -
视图控制器.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;
视图控制器.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];
}