1

I have read several solutions on stackoverflow of which none work for iOS 8.1 I managed to disable a long press from this uiwebview-without-copy-paste-when-displaying-pdf-files but I can't get the popup which offers :"Open", "Add to Reading List" and "Copy" from URLs disabled.

//NSURL *url = [NSURL URLWithString:@"http://www.google.com/somePdf.pdf"];

NSURL *url = [NSURL URLWithString:@"http://www.google.com/"];
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
self.mainWebView.mediaPlaybackRequiresUserAction = NO;
[self.mainWebView.scrollView setBounces:NO];

self.mainWebView.allowsInlineMediaPlayback = YES;
self.mainWebView.delegate = self;

//NSString * jsCallBack = @"window.getSelection().removeAllRanges();";
//[self.mainWebView stringByEvaluatingJavaScriptFromString:jsCallBack];
[self.mainWebView setMultipleTouchEnabled:YES];
[self.mainWebView setScalesPageToFit:YES];
[self.mainWebView loadRequest:requestURL];


UILongPressGestureRecognizer* longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:nil]; // allocating the UILongPressGestureRecognizer

longPress.allowableMovement=100; // Making sure the allowable movement isn't too narrow

longPress.minimumPressDuration=0.3; // This is important - the duration must be long enough to allow taps but not longer than the period in which the scroll view opens the magnifying glass

longPress.delegate=self; // initialization stuff
longPress.delaysTouchesBegan=YES;
longPress.delaysTouchesEnded=YES;

longPress.cancelsTouchesInView=YES; // That's when we tell the gesture recognizer to block the gestures we want

[self.mainWebView addGestureRecognizer:longPress]; // Add the gesture recognizer to the view and scroll view then release
[[self.mainWebView scrollView] addGestureRecognizer:longPress];

I have tested it on some random pdf, and the pdf text is not selectable thanks to the longPress gesture recognizer. However on a normal site it still allows the copy popup.

I will try to edit the web page with this changeing web page settings but the answer is 3 years old...

Maybe I am on a whole wrong track? Should I try using a WKWebView?

Community
  • 1
  • 1
MB_iOSDeveloper
  • 4,178
  • 4
  • 24
  • 36
  • No doubt you should use **WKWebView** at least if you want better performance and access to the latest web technologies **WebKit** supports. – David Gomez Nov 10 '14 at 15:39

3 Answers3

3

There's no way to disable the system's copy/paste popup behaviour in your app, but you can disable this option directly from your web content using -webkit-user-select: none and you should do it only on single elements, not the whole document.

This is how Apple recommends it in one of their Technical Notes:

You can handle touches directly or even detect advanced gestures in Safari on iOS, using the DOM Touch events touchstart, touchmove, touchend, and touchcancel. Unlike mouse events which are emulated, DOM Touch events are specifically designed to work with touch interfaces, so their behavior is reliable and expected. For more information on using touch events in web content for Safari on iOS, see the "Handling Events" section of the Safari Web Content Guide, the Touch, TouchEvent, and TouchList classes in the Safari DOM Additions Reference, and the SlideMe sample code at the Safari Dev Center.

Since touching and holding in Safari on iOS will invoke the Cut/Copy/Paste dialog, you may also choose to disable selection on user interface elements such as menus and buttons using -webkit-user-select: none. It is important to only disable selection as needed on a per-element basis. Selection in webpages should never be globally disabled.

David Gomez
  • 2,762
  • 2
  • 18
  • 28
3

If you load your request to WebView, you can disable the javascript request. In my case, I use the following flow. Firstly you should add a UIWebViewDelegate delegate in a header file.

@interface StoreViewController : HomeButtonController<UIWebViewDelegate>

and then you jump the implementation file and add this code inside the webViewDidFinishLoad delegate method

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

    [webView stringByEvaluatingJavaScriptFromString:
     @"document.documentElement.style.webkitTouchCallout='none';"];
}
Robert Longson
  • 118,664
  • 26
  • 252
  • 242
Emre Gürses
  • 1,992
  • 1
  • 23
  • 28
  • I am showing PDF in webview and this solution doesn't work :( – sanjana Mar 02 '17 at 10:33
  • The above code only disable javascript on web view. What do you want? Showing PDF on web view or disable javascript – Emre Gürses Mar 02 '17 at 11:23
  • I am showing PDF and It has some deeplinks, on which when user do long press system action sheet with option Open/Copy appears. I don't want to show that as links are deep linked within app. – sanjana Mar 02 '17 at 11:27
  • Could you add some information about your ios sdk version, and browser. Depands on your ios sdk or web browser version. – Emre Gürses Mar 02 '17 at 11:50
2

Here is a simple answer that works in iOS 12.0

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        [[UIMenuController sharedMenuController] setMenuVisible:NO animated:NO];
    }];
    return [super canPerformAction:action withSender:sender];
}
ΩlostA
  • 2,501
  • 5
  • 27
  • 63