With iOS 11 Apple has added the ability set add WKWebViews outlets on your nibs and storyboards. It seems to work fine when using the default WKWebViewConfiguration that get set automatically.
However, I'd like to be able to use a custom WKWebViewConfiguration. Is there anyway I can set this before, or after the WKWebView gets initialized from the nib?
First add the web view as a UIWebView outlet in the storyboard / IB. This will give you a property like this: @property (weak, nonatomic) IBOutlet UIWebView *webView; Then just edit your code to change it to a WKWebView.
You can implement WKWebView in Objective-C, here is simple example to initiate a WKWebView : WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init]; WKWebView *webView = [[WKWebView alloc] initWithFrame:self. view. frame configuration:theConfiguration]; webView.
Overview. A WKWebView object is a platform-native view that you use to incorporate web content seamlessly into your app's UI. A web view supports a full web-browsing experience, and presents HTML, CSS, and JavaScript content alongside your app's native views.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With