2013-10-21 30 views
2

我的應用程序是一個IOS手機應用程序。我想從Web視圖的文本框禁用複製和粘貼菜單。長按並雙擊,複製粘貼菜單顯示up.I嘗試禁用長按和雙擊與UIGestureRecognizer類:禁用WebView上文本框的複製和粘貼菜單。我的應用程序基於IOS Phonegap。

- (void)viewDidLoad{ 
UILongPressGestureRecognizer* longPressGesture = [[UILongPressGestureRecognizer alloc]  initWithTarget:self action:@selector(gestureHandler:)]; 
[longPressGesture setMinimumPressDuration:0.2]; 
longPressGesture.delegate = self; 
[self.webView addGestureRecognizer:longPressGesture]; 
} 

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer 
{ 
    if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]) { 
    return NO; 
    } 
    else if([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]){ 
    return NO; 
    } 
    else 
     return YES; 
} 

,但我不能用同樣將其禁用雙clicks.Anyone查詢? 幫我出...

回答

5

那麼你必須寫UIWebView的一個類別,它覆蓋canPerformAction方法,

@implementation UIWebView (DisableCopyPaste) 

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender { 
    UIMenuController *menuController = [UIMenuController sharedMenuController]; 
    if (menuController) { 
     [UIMenuController sharedMenuController].menuVisible = NO; 
    } 
    return NO; 
} 

@end 

此類別中的.PCH文件在Xcode項目文件夾中找到進口,設置斷點指出測試長按事件是否觸發這種方法。

僅供參考此方法可能會被調用很多次,不用擔心它可用於用戶長按的特定UI組件的選項列表。

要創建類別,請按照下列步驟操作。

單擊Xco​​de中項目解決方案瀏覽器底部的添加按鈕。

在選項enter image description here

下一頁選擇目標C類別。

enter image description here

下一頁選擇一個UIWebView或類別文本框鍵入一個UIWebView並給予任何名義的類別 enter image description here

單擊下一步並保存到您的項目位置,類別和複製粘貼上面的功能。瞧!

要禁用HTML中的文本輸入中的複製粘貼或其他選項,請參閱this

+0

我可以知道,如果我們能夠創建在電話間隙網頁視圖的類別。我對它很陌生,所以有查詢電話差距。 – user2903299

+0

沒問題,會走過你。 – satheeshwaran

+0

看我的編輯,讓我知道。 – satheeshwaran

1

添加以下在您的網頁身體或禁止選擇頁面上的UIWebView

.ui-page 
{ 
    -webkit-touch-callout: none; 
    -webkit-user-select: none; 
} 
的/複製CSS
+0

+1,這是一個很好的方法來做到這一點,但不是一直工作。 – satheeshwaran

+0

它也給出了一些問題在ios7 http://stackoverflow.com/questions/11290613/disable-copy-and-paste-in-uiwebview-unsolved/12905995#12905995 – satheeshwaran

+0

其工作在我的ios7應用 – Ved

0
Use below code. 
    <style type="text/css"> 
    *:not(input):not(textarea) { 
     -webkit-user-select: none; /* disable selection/Copy of UIWebView */ 
     -webkit-touch-callout: none; /* disable the IOS popup when long-press on a link */ 
    }  
    </style> 

    If you want Disable only anchor button tag use this. 
    a {-webkit-user-select: none; /* disable selection/Copy of UIWebView */ 
     -webkit-touch-callout: none; /* disable the IOS popup when long-press on a link */ 
    } 
相關問題