decidePolicyForNavigationAction
デリゲートメソッドでは、新しいWKWebView
クラスを宣言し、前のWKWebView
クラスに基づいて、navigationType
またはrequest
に基づくクラス。たとえば、以下のコードスニペットをご覧ください。
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
if (navigationAction.navigationType == WKNavigationTypeLinkActivated) { // OR if (![navigationAction.request.URL.Path isEqualToString:@"Previous URL Path"]) {
// Add cancel button at top of new tab
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(5, 5, 50, 20);
[btn setTitle:@"Close" forState:UIControlStateNormal];
btn.backgroundColor = [UIColor redColor];
[btn addTarget:self action:@selector(close:) forControlEvents:UIControlEventTouchUpInside];
CGRect nFrm = oldWebVw.frame; // Set frame as per your requirement.
nFrm.origin.y = 100;
nFrm.size.height -=120;
newWebVw = [[WKWebView alloc] initWithFrame:nFrm];
newWebVw.backgroundColor = [UIColor blueColor];
[newWebVw addSubview:btn];
[oldWebVw addSubview:newWebVw];
[newWebVw loadRequest:navigationAction.request];
decisionHandler(WKNavigationActionPolicyCancel); // You must cancel the policy else the new request loades on previous WKWebView class
return;
}
decisionHandler(WKNavigationActionPolicyAllow);
}