所以,我做了一些测试:
@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),则该代码会创建 Web 视图,为方案配置它,一切都很好。
方案处理程序:600000008e30
案例一
查看配置:600003d0c780
方案的 URL 处理程序:600000008e30
查看配置:600003d0c780
方案的 URL 处理程序:600000008e30
方案处理程序启动
如果您使用情节提要中设置的 IBOutlet webView 运行该代码(案例 2),那么确实 setURLSchemeHandler:forURLScheme: 不起作用。 urlSchemeHandlerForURLScheme: 的日志在这种情况下返回 nil。
方案处理程序:600000005160
案例 2
查看配置:600003d08d20
方案的 URL 处理程序:0
查看配置:600003d08d20
方案的 URL 处理程序:0
请注意,不调用方案处理程序启动
原因不是您通过 getter 获得了不同的副本,因为配置日志表明它保持不变。只是尽管调用了 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
对我来说工作得很好,但如果你的 web 视图有子视图,可能会很棘手。
查看配置:600003d082d0
方案的 URL 处理程序:600000004c90
查看配置:600003d082d0
方案的 URL 处理程序:600000004c90
方案处理程序启动
请注意,配置通过调用保持不变...
编辑:添加运行日志