Allocate WKWebViewConfiguration to IBOutlet WKWebView

Is it possible to add WKWebViewConfiguration() manually to my WKWebView without instantiation? I ask this because I have an IBOutlet WKWebView from Storyboard(with constrains).

This works (not from Storyboard - manually instantiate):

var myWebView: WKWebView! 
let webConfiguration = WKWebViewConfiguration()
 myWebView = WKWebView(frame: view.bounds, configuration: webConfiguration)

What I want to do is something like this:

@IBOutlet weak var myWebView: WKWebView! 
let webConfiguration = WKWebViewConfiguration() 
myWebView.configuration = webConfiguration //this line does not work

All other stuff is working with an IBOutlet WKWebView, just the configuration are not passing through for JS Bridge e.g.

Someone an idea how to allocate the WKWebViewConfiguration() to my IBOutlet WKWebView?

Accepted Reply

myWebView.configuration = webConfiguration //this line does not work

Indeed. The

configuration
property is read-only; the only way to set it is when you construct the web view via its
init(frame:configuration:)
initialiser, and you can’t do that when you instantiate the web view from a nib or storyboard. If you absolutely have to set the configuration, to configure web-to-native messaging for example, you must construct the web view programmatically.

Fortunately that’s pretty easy. You typically have a custom view controller running the web view anyway, so you can set it in your

loadView()
implementation.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

myWebView.configuration = webConfiguration //this line does not work

Indeed. The

configuration
property is read-only; the only way to set it is when you construct the web view via its
init(frame:configuration:)
initialiser, and you can’t do that when you instantiate the web view from a nib or storyboard. If you absolutely have to set the configuration, to configure web-to-native messaging for example, you must construct the web view programmatically.

Fortunately that’s pretty easy. You typically have a custom view controller running the web view anyway, so you can set it in your

loadView()
implementation.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thank your for quick response eskimo!

Now it is definitly clear for me. I solved this by creating the WKWebView programmatically and set the needed layout constraints via code.

Storyboard IBOutlet is not needed anymore. The Configurations are initialized with the WKWebView.


Cheers


Tobias

I encountered the same issue you had Tobias. I looked everywhere, and even tried this solution in this post. I know this post has been created a year ago, but I thought it would be nice to leave what I found out.

If you add the delegate WKUIDelegate, you have access to "createWebViewWith configuration" and there you are able to change/replace your configuration. This function only triggers when you interact with an UI element on the screen though. In my case I just needed to make links be clickable within the webview. I hope this helps someone out there.
I am trying to get WKUIDelegate to work and cannot get it to trigger my code. I have the protocol implemented in the header <WKNavigationDelegate, WKUIDelegate> and have set the delegates in the loadView:
  • (void)loadView

{
[super loadView];
Code Block
webView.UIDelegate = self;
webView.navigationDelegate = self;
}

But it will not hit my code when the WKWebView loads on the view that should trigger this procedure:
  • (WKWebView*)webView:(WKWebView *)webView createWebViewWithConfiguration:(nonnull WKWebViewConfiguration *)configuration forNavigationAction:(nonnull WKNavigationAction *)navigationAction windowFeatures:(nonnull WKWindowFeatures *)windowFeatures {

Code Block
NSLog(@"IT WORKED!");
return nil;
}

I have no idea what I'm doing wrong. I'm an experienced programmer, but not with XCode, I only use it every once in a while and am mostly a novice.

I'm trying to do what others have asked and want to use StoryBoards and still be able to share a common configuration that will allow shared cookies and sessions from what I understand.