title: Understanding decidePolicyForNavigationResponse with user interaction
body: <p>I would like to get user approval before opening a link using WKWebView.  (Objective C) I am using decidePolicyForNaviagationResponse.  </p>
  
  <p>When it encounters a link in the HTML it should ask using a UIAlertController if it is OK to follow the link or not (in the simplest implementation).  </p>
  
  <p>However it appears to be running asynchronously, so first it opens the link and then eventually gets around to popping up the alert.</p>
  
  <p>How do I encounter the link, pop up the alert and THEN either open the link or not.  I’m guessing either something about blocks that I don’t understand like a completion handler or  perhaps using semaphores, although my modest attempts at them didn’t work.</p>
  
  <p>I have simplified the code to make it clear what’s happening.</p>
  
  <p>Thank you!</p>
  
  <pre><code>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;    
  }
  </code></pre>