【问题标题】:How to disable copy share option while selecting text in uiwebview? But selecting text should work在uiwebview中选择文本时如何禁用复制共享选项?但是选择文本应该可以
【发布时间】:2016-06-23 18:08:37
【问题描述】:

我想限制用户在UIWebView 中复制和共享文本。

我使用的代码是

- (void)viewDidLoad {
[super viewDidLoad];
self.webview.delegate = self;
NSString *path = [[NSBundle mainBundle] pathForResource:@"SampleHTML" ofType:@"html"];
NSString *htmlContent = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[self.webview loadHTMLString:htmlContent baseURL:[NSURL URLWithString:@""]];

// Do any additional setup after loading the view, typically from a nib.

}

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

else if (action == @selector(select:))
return YES;

return [super canPerformAction:action withSender:sender];
}

【问题讨论】:

    标签: ios uiwebview selection copy-paste


    【解决方案1】:

    您需要做的第一件事是添加带有 UIWEBVIEW 子类的新类。

    将此代码粘贴到您班级的 .m 文件中。

    @implementation UIWebView (Additional)
    
    - (BOOL)canPerformAction:(SEL)action withSender:(id)sender
    {
        BOOL superCanPerform = [super canPerformAction:action withSender:sender];
        if (superCanPerform) {
            if (action == @selector(copy:) ||
                action == @selector(paste:)||
                action == @selector(cut:)||
                action == @selector(_share:))
            {
                return false;
            }
        }
        return superCanPerform;
    }
    

    试试这个,这将隐藏所有需要的子菜单。

    【讨论】:

      【解决方案2】:

      用代码试试,这对我有用

      - (void)webViewDidFinishLoad:(UIWebView *)webView 
      {
           [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"];
      }
      

      要禁用共享,

      - (BOOL)canPerformAction:(SEL)action withSender:(id)sender { 
      if (action == @selector(customMethod:)) {
          return [super canPerformAction:action withSender:sender];
      }
      return NO;
      }
      

      希望这对你有用。

      【讨论】:

      • 我想让选择选项工作,只复制和分享我想禁用。
      • 立即查看 :) 您还可以从这里获得更多选择:stackoverflow.com/questions/5995210/…
      • 不,它仍然允许用户复制文本 inuiwebview。
      • 不,它在这里不起作用。我已经获取了示例 html 并在 uiwebview 中调用。我已经编辑了上面的代码。
      • 我也添加了你的代码,但是没有执行。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-11
      • 1970-01-01
      • 1970-01-01
      • 2011-11-04
      • 1970-01-01
      • 1970-01-01
      • 2013-05-02
      相关资源
      最近更新 更多