2016-06-23 5 views
2

UIWebViewのテキストのコピーと共有を制限したいのですが。uiwebviewでテキストを選択中にコピー共有オプションを無効にするにはどうすればよいですか?しかし、テキストを選択するとうまくいくはずです

私が使用しているコードは、これが

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

は、共有を無効にするには私の作品、

- (void)viewDidLoad { 
[super viewDidLoad]; 
self.webview.delegate = self; 
NSString *path = [[NSBundle mainBundle] pathForResource:@"SampleHTML" ofType:@"html"]; 
NSString *htmlContent = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; 
[self.webview loadHTMLString:htmlContent baseURL:[NSURL URLWithString:@""]]; 

// Do any additional setup after loading the view, typically from a nib. 

}

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender 
{ 
if (action == @selector(copy:)) 
return NO; 

else if (action == @selector(select:)) 
return YES; 

return [super canPerformAction:action withSender:sender]; 
} 

答えて

3

最初に行う必要があるのは、新しいクラスをUIWEBVIEWのサブクラスで追加することです。

このコードをクラスの.mファイルに貼り付けます。

@implementation UIWebView (Additional) 

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender 
{ 
    BOOL superCanPerform = [super canPerformAction:action withSender:sender]; 
    if (superCanPerform) { 
     if (action == @selector(copy:) || 
      action == @selector(paste:)|| 
      action == @selector(cut:)|| 
      action == @selector(_share:)) 
     { 
      return false; 
     } 
    } 
    return superCanPerform; 
} 

これを試すと、必要なすべてのサブメニューが非表示になります。

+0

完璧!ありがとうございます.. –

+0

あなたの歓迎:) – Rupinder

0

はコードで試している

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender { 
if (action == @selector(customMethod:)) { 
    return [super canPerformAction:action withSender:sender]; 
} 
return NO; 
} 

これはあなたのために働くことを望みます。

+0

私は選択オプションを働かせたいと思います。コピーして共有するだけです。 –

+0

今すぐ確認:)また、ここからより多くのオプションを得ることができます:http://stackoverflow.com/questions/5995210/disabling-user-selection-in-uiwebview –

+0

それでも、ユーザーはテキストinuiwebviewをコピーすることができます。 –

関連する問題