UIWebView without Copy/Paste when displaying PDF files

前端 未结 8 961
醉话见心
醉话见心 2020-12-10 20:21

I have tried to disable Copy/Paste in UIWebView by using a category and overriding canPerformAction and returning NO for copy, cut and paste selectors.

It worked

8条回答
  •  独厮守ぢ
    2020-12-10 20:39

    Looking for a Xamarin.iOS solution.

    var longPressGestureRecognizer = new CustomLongPressGestureRecognizer ((UILongPressGestureRecognizer obj) => 
    {
      Console.WriteLine ("CustomLongPressGestureRecognizer action");
    });
    webView.AddGestureRecognizer (longPressGestureRecognizer);
    

    The approach given by Zubaba might look like this:

    public class ZubabaLongPressGestureRecognizer : UIKit.UILongPressGestureRecognizer
    {
        public ZubabaLongPressGestureRecognizer (Action action)
          : base (action)
        {
            AllowableMovement = 100;
            MinimumPressDuration = 0.3;
            DelaysTouchesBegan = true;
            DelaysTouchesEnded = true;
            CancelsTouchesInView = true;
        }
    }
    

    The open/copy/cancel menu still shows the first time a link is long held per PDF page. After that first long press, however, it properly does not show up for that page. That this is PDF page dependent does not give me confidence that there is a solution available.

    Johnny Rockex's solutions might look like this:

    public class RockexLongPressGestureRecognizer : UIKit.UILongPressGestureRecognizer
    {
        public RockexLongPressGestureRecognizer(UIKit.UIWebView webView, Action action) :
            base(UserInteractionAction(webView) + action)
        {
    
        }
    
        private static Action UserInteractionAction(UIKit.UIWebView webView)
        {
            return (UILongPressGestureRecognizer obj) =>
             {
                 webView.UserInteractionEnabled = false;
                 webView.UserInteractionEnabled = true;
             };
        }
    
        public override bool CanPreventGestureRecognizer(UIGestureRecognizer preventedGestureRecognizer)
        {
            return false;
        }
    }
    

    and

    notificationToken1 = UIMenuController.Notifications.ObserveMenuFrameDidChange (Callback);
    notificationToken2 = NSNotificationCenter.DefaultCenter.AddObserver(UIMenuController.DidShowMenuNotification, Callback);
    

    I was not able to get either to do anything. Helpfully someone else has done better with a Xamarin.iOS fix

提交回复
热议问题