Skip to content

Instantly share code, notes, and snippets.

@wenghengcong
Created May 17, 2019 01:36
Show Gist options
  • Select an option

  • Save wenghengcong/9286673c8267e683e7c46afc595d04c6 to your computer and use it in GitHub Desktop.

Select an option

Save wenghengcong/9286673c8267e683e7c46afc595d04c6 to your computer and use it in GitHub Desktop.
WKWebView
NSString *backGroundColorSource = @"document.body.style.background = \"#ffffff\"";
WKUserScript *backGroundColorUserScript = [[WKUserScript alloc] initWithSource:backGroundColorSource injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
WKUserContentController *userController = [[WKUserContentController alloc] init];
[userController addUserScript:backGroundColorUserScript];
WKPreferences *preference = [[WKPreferences alloc] init];
preference.minimumFontSize = 25;
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
configuration.userContentController = userController;
configuration.preferences = preference;
self.webView = [[WKWebView alloc] initWithFrame:self.containView.frame configuration:configuration];
[self.containView addSubview:self.webView];
// [self.containView mas_remakeConstraints:^(MASConstraintMaker *make) {
// make.left.top.right.bottom.mas_equalTo(0);
// }];
//
[self.containView bringSubviewToFront:self.progressBar];
[self.webView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.bottom.left.right.mas_equalTo(0);
}];
// 监听加载进度,以及webview标题
[self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:NULL];
[self.webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
self.webView.UIDelegate = self;
self.webView.navigationDelegate = self;
设置代理
// 跳转新页面
- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures
{
NSLog(@"createWebViewWithConfiguration");
if (!navigationAction.targetFrame.isMainFrame) {
[webView loadRequest:navigationAction.request];
}
return nil;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment