Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set WKWebViewConfiguration on WKWebView from Nib or Storyboard

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?

like image 999
Matt.M Avatar asked Jan 25 '18 20:01

Matt.M


People also ask

How do I add WKWebView to my storyboard?

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.

How do I import WKWebView into Objective C?

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.

What is a WKWebView?

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.


3 Answers

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; 
like image 71
Ravi Kumar Avatar answered Oct 18 '22 15:10

Ravi Kumar


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)
    }
}
like image 43
Angolao Avatar answered Oct 18 '22 14:10

Angolao


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

like image 28
guru Avatar answered Oct 18 '22 16:10

guru