开发者

UIWebView - creating custom context menu?

开发者 https://www.devze.com 2023-04-03 06:43 出处:网络
I added UIWebView control to my application. To disable default context menu, I implemented webViewDidFinishLoad.

I added UIWebView control to my application.

To disable default context menu, I implemented webViewDidFinishLoad.

- (void) webViewDidFinishLoad:(UIWebView *)theWebView {
    NSString *varMySheet = @"var mySheet = document.styleSheet[0];";
    NSString *addCSSRule = @"function addCSSRule(selector, newRule) {"
        "if (mySheet.addRule)开发者_StackOverflow社区 {"
        "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 context menu of webview doesn't disappear. Anyone help me.

I also tried

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

It didn't work. Thanks.


Could you explain why you are trying to do with all this javascript ? Just doing the following is not enough for you ?

- (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';"];
}


You just need to subclass the UIWebView. In your custom view, just implement the method canPerformAction:withSender like this:

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

then all menu items will disappear. If you just wanna show some of items, you should return YES for the specified item.

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

That gives you only "copy" when you long press a word in the view.

0

精彩评论

暂无评论...
验证码 换一张
取 消