UIWebView without Copy/Paste when displaying PDF files

前端 未结 8 947
醉话见心
醉话见心 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:41

    1.ios11 iphone6 Object-C Without Copy/Paste/lookUp/share

    2.

    viewDidLoad{
        .......
        [self setupExclude];
    }
    
    - (void)setupExclude{
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPG)];
        longPress.minimumPressDuration = 0.2;
        [self.webview addGestureRecognizer:longPress];
    
        UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:nil];
        singleTapGesture.numberOfTapsRequired = 1;
        singleTapGesture.numberOfTouchesRequired  = 1;
        [self.webview addGestureRecognizer:singleTapGesture];
    
        UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(longPG)];
        doubleTapGesture.numberOfTapsRequired = 2;
        doubleTapGesture.numberOfTouchesRequired = 1;
        [self.webview addGestureRecognizer:doubleTapGesture];
        [singleTapGesture requireGestureRecognizerToFail:doubleTapGesture];
    }
    - (BOOL)canPerformAction:(SEL)action withSender:(id)sender{
        BOOL res = [super canPerformAction:action withSender:sender];
        UIMenuController.sharedMenuController.menuVisible = NO;
        self.webview.userInteractionEnabled = NO;
        self.webview.userInteractionEnabled = YES;
        return res;
    }
    - (void)longPG{
        UIMenuController.sharedMenuController.menuVisible = NO;
        self.webview.userInteractionEnabled = NO;
        self.webview.userInteractionEnabled = YES;
    }
    

    3. Done!

提交回复
热议问题