Итак, я сделал несколько тестов:
@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
Если вы запускаете этот код с неустановленным параметром IBOutlet webView, установленным в раскадровке (вариант 1), то код создает веб-представление, настраивает его для схемы и все в порядке.
обработчик схемы: 600000008e30
Дело 1
Просмотр конфигурации: 600003d0c780
Обработчик URL для схемы: 600000008e30
Просмотр конфигурации: 600003d0c780
Обработчик URL для схемы: 600000008e30
схема запуска обработчика
Если вы запускаете этот код с веб-представлением IBOutlet, установленным в раскадровке (случай 2), то действительно, setURLSchemeHandler: forURLScheme: не работает. Журнал urlSchemeHandlerForURLScheme: в этом случае возвращает nil.
обработчик схемы: 600000005160
Дело 2
Просмотр конфигурации: 600003d08d20
Обработчик URL для схемы: 0
Просмотр конфигурации: 600003d08d20
Обработчик URL для схемы: 0
Обратите внимание, что запуск обработчика схемы не называется
Причина не в том, что вы получаете другую копию через геттер, так как журнал конфигурации показывает, что она остается прежней. Просто обработчик схемы не установлен, несмотря на вызов setURLSchemeHandler: forURLScheme.
Так что я думаю, что единственное решение - использовать вариант 1. В зависимости от настроек вашего вида, может быть более или менее сложно вставить вид. Я бы предложил иметь на вашей доске объявлений пустой вид "мать" и использовать следующий код:
@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
Работает нормально для меня, но может быть сложно, если у вас есть подпредставления для вашего веб-представления.
Просмотр конфигурации: 600003d082d0
Обработчик URL для схемы: 600000004c90
Просмотр конфигурации: 600003d082d0
Обработчик URL для схемы: 600000004c90
схема запуска обработчика
Обратите внимание, что конфигурация остается неизменной посредством вызовов ...
Редактировать: добавлены журналы запуска