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

时间:2019-02-08 13:04:18

标签: ios objective-c xcode wkwebview

我在WKWebView中拦截某些HTTP请求时遇到问题(我使用目标C)。似乎未调用definePolicyForNavigationAction。有人可以帮我解决我可能做错的事情吗?

我首先从库中将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);
    }
}

2 个答案:

答案 0 :(得分:0)

更改此:

[webView loadRequest:urlReq];

对此:

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

答案 1 :(得分:0)

好吧,我终于找到了问题:) 此属性阻止了我的NSLog:“ OS_ACTIVITY_MODE”:“禁用