通过用户交互了解DecisionPolicyForNavigationResponse

时间:2019-02-10 21:23:06

标签: objective-c wkwebview uialertcontroller

在使用WKWebView打开链接之前,我想获得用户的认可。 (目标C)我正在使用definePolicyForNaviagationResponse。

当它遇到HTML中的链接时,应使用UIAlertController询问是否可以跟踪该链接(在最简单的实现中)。

但是它似乎是异步运行的,因此,它首先打开链接,然后最终弹出警报。

我如何遇到该链接,弹出警报,然后打开或不打开该链接。我正在猜测一些我不了解的块,例如完成处理程序,或者可能使用了信号量,尽管我的适度尝试没有用。

我简化了代码,以清楚说明正在发生的事情。

谢谢!

static bool launchPermission = false;
@property (strong, nonatomic) WKWebViewConfiguration *theConfiguration;
@property (strong, nonatomic) WKWebView *webView;

.
.
.

_webView.navigationDelegate = self;
[_webView  loadRequest:nsrequest];
[self.view addSubview:_webView];

.
.
.

- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{

        [self askPermissionForExternalLink];
        if (launchPermission)
        {
            decisionHandler(WKNavigationResponsePolicyAllow);
        }
        else
        {
            decisionHandler(WKNavigationResponsePolicyCancel);
        }
}


- (void) askPermissionForExternalLink
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Open external Web Conten?" message:@"Link is embedded with other content" preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *cancelAction = [UIAlertAction
                                   actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel action")
                                   style:UIAlertActionStyleCancel
                                   handler:^(UIAlertAction *action)
                                   {
                                       NSLog(@"Cancel action");
                                       [self cancelMethod];
                                       //return;
                                   }];

    UIAlertAction *okAction = [UIAlertAction
                               actionWithTitle:NSLocalizedString(@"OK", @"OK action")
                               style:UIAlertActionStyleDefault
                               handler:^(UIAlertAction *action)
                               {
                                   NSLog(@"OK action");
                                   //[self launchURL];
                                   [self OKMethod];
                               }];

    [alert addAction:cancelAction];
    [alert addAction:okAction];
    [alert show];
}

- (bool) cancelMethod
{
    launchPermission = false;
    return false;   
}

- (bool) OKMethod
{
    launchPermission = true;
    return true;    
}

1 个答案:

答案 0 :(得分:1)

您可以先在decidePolicyForNavigationResponse的make块中使用define来尝试使用这种方法

- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{



       void (^ launchPermission)(BOOL) = ^(BOOL isAllow)
        {
            if(isAllow)
            {
                decisionHandler(WKNavigationActionPolicyAllow);
            }
            else
            {
                decisionHandler(WKNavigationActionPolicyCancel);
            }
            return;
        };
        [self askPermissionForExternalLink];

    }

此处根据用户选择,将YES或NO发送到launchPermission块

- (void) askPermissionForExternalLink
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Open external Web Conten?" message:@"Link is embedded with other content" preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *cancelAction = [UIAlertAction
                                   actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel action")
                                   style:UIAlertActionStyleCancel
                                   handler:^(UIAlertAction *action)
                                   {
                                       NSLog(@"Cancel action");
                                       launchPermission(NO);
                                       return;
                                   }];

    UIAlertAction *okAction = [UIAlertAction
                               actionWithTitle:NSLocalizedString(@"OK", @"OK action")
                               style:UIAlertActionStyleDefault
                               handler:^(UIAlertAction *action)
                               {
                                   NSLog(@"OK action");

                                   launchPermission(YES);
                                  return ;
                               }];

    [alert addAction:cancelAction];
    [alert addAction:okAction];
    [alert show];
}