UIWebView - creating a custom context menu?

I added a UIWebView control to my application.

To disable the default context menu, I applied webViewDidFinishLoad.

- (void) webViewDidFinishLoad:(UIWebView *)theWebView {
    NSString *varMySheet = @"var mySheet = document.styleSheet[0];";
    NSString *addCSSRule = @"function addCSSRule(selector, newRule) {"
        "if (mySheet.addRule) {"
        "mySheet.addRule(selector, newRule);"
        "} else {"
        "ruleIndex = mySheet.cssRules.length;"
        "mySheet.insertRule(selector + '{' + newRule + ';}', ruleIndex;"
        "}"
        "}";
    ...
    NSString *insertRule = @"addCSSRule('body', '-webkit-touch-callout: none;')";

    [webView stringByEvaluatingJavaScriptFromString:varMySheet];
    [webView stringByEvaluatingJavaScriptFromString:addCSSRule];
    [webView stringByEvaluatingJavaScriptFromString:insertRule];
    ...
}

But the webview context menu does not disappear. Someone help me.

I also tried

[webView stringByEvaluatingJavaScriptFromString:@"document.body.style.webkitTouchCallout='none';"];

This did not work. Thank.

+4
source share
2 answers

Could you explain why you are trying to do all this javascript? Is it simply not enough for you to follow these steps?

- (void) webViewDidFinishLoad:(UIWebView *) sender {
    // Disable the defaut actionSheet when doing a long press
    [webView stringByEvaluatingJavaScriptFromString:@"document.body.style.webkitTouchCallout='none';"];
    [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"];
}
0
source

You just need to subclass UIWebView. In your custom view, simply implement the canPerformAction: withSender method as follows:

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
  return NO;
}

. , YES .

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
  BOOL ret = NO;
  if (action == @selector(copy:)) ret = YES;
  return ret;
}

"", .

0

All Articles