title: Dismiss copy/paste contextual menu and remove selection in UIWebView
body: <p>I have a UIWebView in which I've implemented a single tap gesture recognizer. I don't have any recognizer for long hold, though my problem is in making a selection. Issues:</p>
  
  <ul>
  <li>Select some text in a UIWebView, select copy from contextual menu. Menu should disappear.</li>
  <li>Long hold to make a text selection, single tap gesture is also getting called.</li>
  <li>If text is selected, a single tap elsewhere should remove the selection like in Safari.</li>
  </ul>
  
  <p>…</p>
  
  <pre><code>func createGestureRecognizer() {
      // single tap
      let singleTapGesture = UITapGestureRecognizer(target: self, action: "handleSingleTap:")
      singleTapGesture.numberOfTapsRequired = 1
      singleTapGesture.delegate = self
      singleTapGesture.cancelsTouchesInView = false
      webView.addGestureRecognizer(singleTapGesture)
  }
  
  func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -&gt; Bool {
      if touch.view.isDescendantOfView(self.webView) {
          return true
      }
      return true
  }
  
  func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -&gt; Bool {
      if otherGestureRecognizer.isKindOfClass(UITapGestureRecognizer) {
          otherGestureRecognizer.requireGestureRecognizerToFail(gestureRecognizer)
          // println("added failure requirement to: \(otherGestureRecognizer)")
      }
      return true
  }
  
  func handleSingleTap(tap: UITapGestureRecognizer) {
      …
  }
  </code></pre>
  
  <p><strong>EDIT:</strong> It is sort of working. Problem: Making an initial text selection requires a longer hold than normal. A normal long hold that would normally make a selection calls my single tap recognizer without selecting any text.</p>
  
  <pre><code>func handleSingleTap(tap: UITapGestureRecognizer) {
      let selection = webView.stringByEvaluatingJavaScriptFromString("window.getSelection().toString()")!
      if selection == "" {
          // do stuff
      } else {
          webView.userInteractionEnabled = false
          webView.userInteractionEnabled = true
          webView.stringByEvaluatingJavaScriptFromString("window.getSelection().removeAllRanges()")
      }
  </code></pre>