-
-
Save wenghengcong/9286673c8267e683e7c46afc595d04c6 to your computer and use it in GitHub Desktop.
WKWebView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 设置代理 | |
| // 跳转新页面 | |
| - (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