Domanda

In my ViewController's header file I have:

@interface CaseStudyChoiceViewController : UIViewController <UIWebViewDelegate>

@property (strong, nonatomic) IBOutlet UIWebView *myWebView;

@end

Then in the .m file in the ViewDidLoad method:

[_myWebView setDelegate:self];

and finally:

#pragma mark - WebView Delegate Methods

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(copy:)   || 
        action == @selector(paste:)  || 
        action == @selector(select:) || 
        action == @selector(cut:)    || 
        action == @selector(selectAll:))
    {
        NSLog(@"Selector is %@", NSStringFromSelector(action));
        return NO;
    }
    else
    {
        return [super canPerformAction:action withSender:sender];
    }

   //I tried the code below as well and got the same result
   /*
    UIMenuController *menuController = [UIMenuController sharedMenuController];
    if (menuController) 
    {
        [UIMenuController sharedMenuController].menuVisible = NO;
    }
    return NO;
   */
}

The problem I have is that this will NOT disable the Copy function. When I run this the NSLog output is:

    2013-12-30 21:39:28.794 PCO - CLS[45238:70b] Selector is cut:
    2013-12-30 21:39:28.794 PCO - CLS[45238:70b] Selector is select:
    2013-12-30 21:39:28.795 PCO - CLS[45238:70b] Selector is selectAll:
    2013-12-30 21:39:28.795 PCO - CLS[45238:70b] Selector is paste:

copy: is never presented in the canPerformAction method. Any ideas on where I can capture it?

The end result here is that when the user does a long touch in the UIWebView two options pop up, Copy and Define. I only want Define.

È stato utile?

Soluzione 2

Solved!

Ok, so the solution described mid-page here: Disabling user selection in UIWebView by TPoschel works as advertised...until you load a .pdf file into your UIWebView. Loading a .pdf results in the copy/define menu popping up again once the user performs a long touch in the UIWebView.

After another bout of hair pulling I found this answer on here by Johnny Rockex and it worked like a champ. UIWebView without Copy/Paste when displaying PDF files

Many thanks to him for this easy to implement, genius solution!! In fact this should be a universal solution, and work across the board without having to use webkit.

Altri suggerimenti

it work ios 7.0 -> 9.2

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

[webView stringByEvaluatingJavaScriptFromString:
   @"document.documentElement.style.webkitTouchCallout='none';"];
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top