【问题标题】:Disable copy and paste menu from the textfield on a WebView. My app is based on IOS Phonegap.从 WebView 的文本字段中禁用复制和粘贴菜单。我的应用程序基于 IOS Phonegap。
【发布时间】:2013-10-30 00:57:05
【问题描述】:

我的应用是一个 ios phonegap 应用。我想从 Web 视图的文本字段中禁用复制和粘贴菜单。长按和双击时,会显示复制粘贴菜单。我尝试使用 UIGestureRecognizer 类禁用长按和双击:

- (void)viewDidLoad{
UILongPressGestureRecognizer* longPressGesture = [[UILongPressGestureRecognizer alloc]     initWithTarget:self action:@selector(gestureHandler:)];
[longPressGesture setMinimumPressDuration:0.2];
longPressGesture.delegate = self;
[self.webView addGestureRecognizer:longPressGesture];
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer    shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer   *)otherGestureRecognizer
{
    if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]) {
    return  NO;
    }
    else if([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]){
    return  NO;
    }
    else
        return YES;
}

但我无法禁用它进行双击。有相同查询的人吗? 帮帮我...

【问题讨论】:

    标签: ios cordova webview long-press


    【解决方案1】:

    在您的网页正文或页面上添加以下 css 以禁用 UIWebView 的选择/复制

    .ui-page 
    { 
       -webkit-touch-callout: none; 
       -webkit-user-select: none; 
    }
    

    【讨论】:

    • +1,这是一个很好的方法,但并不总是有效。
    • 而且它在 ios7 stackoverflow.com/questions/11290613/…987654321@ 中也给出了一些问题
    • 这会禁用我的输入字段,键盘会显示但无法在该字段中输入任何内容
    • 使用人行横道(使用 Blink),-webkit-touch-callout: none 没有效果,但 -webkit-user-select: none 工作正常。可惜他们两个都不标准。
    【解决方案2】:
      Use below code.
      <style type="text/css">
        *:not(input):not(textarea) {
           -webkit-user-select: none; /* disable selection/Copy of UIWebView */
           -webkit-touch-callout: none; /* disable the IOS popup when long-press on a link */
        }       
      </style>
    
      If you want Disable only anchor button tag use this.
       a {-webkit-user-select: none; /* disable selection/Copy of UIWebView */
          -webkit-touch-callout: none; /* disable the IOS popup when long-press on a link */
      }
    

    【讨论】:

      【解决方案3】:

      你必须为 UIWebView 编写一个覆盖 canPerformAction 方法的类别,

      @implementation UIWebView (DisableCopyPaste)
      
      -(BOOL)canPerformAction:(SEL)action withSender:(id)sender {
          UIMenuController *menuController = [UIMenuController sharedMenuController];
          if (menuController) {
              [UIMenuController sharedMenuController].menuVisible = NO;
          }
          return NO;
      }
      
      @end
      

      在你的xcode项目文件夹下的.pch文件中导入这个类别,设置断点来测试长按事件是否触发这个方法。

      仅供参考,此方法可能会被多次调用,不要担心它是针对用户长按的特定 UI 组件可用的选项列表。

      要创建类别,请按照以下步骤操作。

      在 Xcode 中单击项目解决方案浏览器底部的添加按钮。

      下一步在选项中选择Objective C Category。

      下一步选择 UIWebView 或在文本框的类别中输入 UIWebView 并为类别命名

      单击下一步并将类别保存到您的项目位置并复制粘贴上述功能。瞧!。

      要在 HTML 中的文本输入中禁用复制粘贴或其他选项,请参阅this

      【讨论】:

      • 我可以知道,如果我们可以在电话间隙中为 web 视图创建一个类别。我是新手,所以有电话差距的问题。
      • 没问题,会带你过去的。
      • 感谢 satheeshwaram 的建议。我仍然无法解决问题,我仍然在 webview 上获得相同的复制和粘贴菜单
      • 您是否在 .pch 文件中导入了此类别?长按动作是否触发了此事件?
      • 是的,我有,当长按事件发生时,该功能会被击中。函数返回 false 仍然弹出复制粘贴菜单
      猜你喜欢
      • 2013-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-30
      • 2011-03-05
      • 2019-07-11
      • 2017-02-08
      相关资源
      最近更新 更多