Set WKWebViewConfiguration on WKWebView from Nib or Storyboard
Let's Example your customize configuration.
NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
//Here you can customize configuration
[self.webView.configuration.userContentController addUserScript:wkUScript];
// self.webView.navigationDelegate = self;
// self.webView.contentMode = UIViewContentModeScaleAspectFill;
You can create WKWebView in Storyboard and then using WKUIDelegate:createWebViewWith
for creating new WKWebView with configuration alone.
class WindowViewController: UIViewController {
// The WKWebView created from storyboard
@IBOutlet var webView: WKWebView!
override func viewDidLoad() {
// Delegate the webview's uiDelegate
self.webView.uiDelegate = self
}
}
extension WindowViewController: WKUIDelegate {
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
// Create new WKWebView with custom configuration here
let configuration = WKWebViewConfiguration()
return WKWebView(frame: webView.frame, configuration: configuration)
}
}
Yes!!! We can set WKWebViewConfiguration on WKWebView from Nib or Storyboard I am using below code for that:
Here is my IBOutlet--
@IBOutlet var webViewChat: WKWebView!
//Using below code you can set configuration
webViewChat.configuration.userContentController.add(self, name: "edit")
webViewChat.configuration.userContentController.add(self, name: "remove")
Note : Make sure you load URL after setting up the configuration