尝试根据 WKWebView 中的当前 URL 从外部打开 PDF,但未触发 decidePolicyForNavigationAction

Trying to open a PDF externally based upon what the current URL in WKWebView is, but decidePolicyForNavigationAction is not fired

我在拦截 WKWebView 中的某些 HTTP 请求时遇到问题(我使用 objective C)。 decidePolicyForNavigationAction 似乎没有被调用。谁能帮我看看我做错了什么?

我首先从库中添加一个 WKWebview 组件到我的 Main.Storyboard

然后我将以下内容添加到我的 ViewController.h

#import <WebKit/WebKit.h>

@interface ViewController : UIViewController<UIAlertViewDelegate, WKUIDelegate, WKNavigationDelegate>

@property (strong, nonatomic) IBOutlet WKWebView *webView;

@end

这对 ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize webView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    webView = [[WKWebView alloc] initWithFrame:[[self view] bounds]];
    NSURL *url = [NSURL URLWithString:@"https://url.com"];
    NSURLRequest *urlReq = [NSURLRequest requestWithURL:url];

    [webView loadRequest:urlReq];
    self.view = webView;
}


- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    NSLog(@"decidePolicyForNavigationAction");
    if (decisionHandler) {
        NSLog(@"decidePolicyForNavigationAction2");
        decisionHandler(WKNavigationActionPolicyAllow);
    }
}

改变这个:

[webView loadRequest:urlReq];

为此:

webView.navigationDelegate = self;
[webView loadRequest:urlReq];

嗯,终于找到问题了:) 这个 属性 阻止了我的 NSLog 的:"OS_ACTIVITY_MODE": "disable