So, I did some tests :
@interface ViewController ()
@property (strong) IBOutlet WKWebView *webView ;
@end
@implementation ViewController
- (void) awakeFromNib
{
static BOOL hasBeenDone = NO ; // because otherwise, awakeFromNib is called twice
if (!hasBeenDone)
{
hasBeenDone = YES ;
// Create the scheme handler
SchemeHandler *mySchemeHandler = [SchemeHandler new] ;
NSLog(@"scheme handler : %lx",mySchemeHandler) ;
if (!(self.webView))
{
// Case 1 - we don't have a web view from the StoryBoard, we need to create it
NSLog(@"Case 1") ;
// Add the scheme
WKWebViewConfiguration *configuration = [WKWebViewConfiguration new] ;
[configuration setURLSchemeHandler:mySchemeHandler
forURLScheme:@"stef"] ;
// Create and set the web view
self.webView = [[WKWebView alloc] initWithFrame:NSZeroRect
configuration:configuration] ;
}
else
{
// Case 2 - we have a web view from the story board, just set the URL scheme handler
// of the configuration
NSLog(@"Case 2") ;
WKWebViewConfiguration *configuration = self.webView.configuration ;
[configuration setURLSchemeHandler:mySchemeHandler
forURLScheme:@"stef"] ;
}
// Log the view configuration
NSLog(@"View configuration : %lx",self.webView.configuration) ;
NSLog(@"URL handler for scheme : %lx",[self.webView.configuration urlSchemeHandlerForURLScheme:@"stef"]) ;
}
}
- (void) viewDidLoad
{
[super viewDidLoad] ;
// Log the view configuration
NSLog(@"View configuration : %lx",self.webView.configuration) ;
NSLog(@"URL handler for scheme : %lx",[self.webView.configuration urlSchemeHandlerForURLScheme:@"stef"]) ;
// Start loading a URL with the scheme - this should log "start" if everything works fine
NSURL *url = [NSURL URLWithString:@"stef://willIWinTheBounty?"] ;
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url] ;
[self.webView loadRequest:urlRequest] ;
}
@end
If you run that code with the IBOutlet webView unset set in the storyboard (Case 1), then the code creates the web view, configures it for the scheme and everything is fine.
scheme handler : 600000008e30
Case 1
View configuration : 600003d0c780
URL handler for scheme : 600000008e30
View configuration : 600003d0c780
URL handler for scheme : 600000008e30
scheme handler start
If you run that code with the IBOutlet webView set in the storyboard (Case 2), then indeed, setURLSchemeHandler:forURLScheme: is not working. The log of urlSchemeHandlerForURLScheme: returns nil in that case.
scheme handler : 600000005160
Case 2
View configuration : 600003d08d20
URL handler for scheme : 0
View configuration : 600003d08d20
URL handler for scheme : 0
Note that scheme handler start is not called
The reason is not that you get a different copy through the getter, as the log of configuration indicates it remains the same. It is just that the scheme handler is not set despite the call to setURLSchemeHandler:forURLScheme.
So I guess the only solution is to use case 1. Depending on your view setup, it might be more or less difficult to insert the view. I would suggest to have in your story board an empty "mother" view, and use the following code :
@interface ViewController ()
@property (weak) IBOutlet NSView *webViewMother ;
@property (strong) WKWebView *webView ;
@end
@implementation ViewController
- (void) awakeFromNib
{
static BOOL hasBeenDone = NO ; // because otherwise, awakeFromNib is called twice
if (!hasBeenDone)
{
hasBeenDone = YES ;
// Create the scheme handler
SchemeHandler *mySchemeHandler = [SchemeHandler new] ;
// Create the configuration
WKWebViewConfiguration *configuration = [WKWebViewConfiguration new] ;
[configuration setURLSchemeHandler:mySchemeHandler
forURLScheme:@"stef"] ;
// Create the web view at the size of its mother view
self.webView = [[WKWebView alloc] initWithFrame:self.webViewMother.frame
configuration:configuration] ;
[self.webViewMother addSubview:self.webView] ;
// Log the view configuration
NSLog(@"View configuration : %lx",self.webView.configuration) ;
NSLog(@"URL handler for scheme : %lx",[self.webView.configuration urlSchemeHandlerForURLScheme:@"stef"]) ;
}
}
@end
Working fine for me, but could be tricky if you have subviews to your web view.
View configuration : 600003d082d0
URL handler for scheme : 600000004c90
View configuration : 600003d082d0
URL handler for scheme : 600000004c90
scheme handler start
Note that configuration remains the same through calls...
Edit : added the run logs