124

コンテンツをロードしてUIWebViewこれを提示するアプリがあります。ユーザーがリンクをクリックできるようにしたいので、ユーザーの操作を完全に無効にすることはできません。ユーザーの選択を無効にするだけです。私はあなたが使用できるインターネットのどこかを見つけました:

document.body.style.webkitUserSelect='none';

これを次のように挿入してみました

[self.contentView stringByEvaluatingJavaScriptFromString:@"document.body.style.webkitUserSelect='none';"]; 

webViewDidFinishLoad:

しかし、うまくいきません。WebView 内のテキストを選択してコピーすることはできます。

何がうまくいかないのでしょうか?

更新: これは iOS 4.3 以降でのみ発生するようです

4

12 に答える 12

281

選択を無効にするいくつかの方法を次に示します。

以下をモバイル Web ドキュメントに追加します。

<style type="text/css">
* {
    -webkit-touch-callout: none;
    -webkit-user-select: none; /* Disable selection/copy in UIWebView */
}
</style>

次の Javascript コードをプログラムでロードします。

NSString * jsCallBack = @"window.getSelection().removeAllRanges();";    
[webView stringByEvaluatingJavaScriptFromString:jsCallBack];

コピー/貼り付けユーザー メニューを無効にします。

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender 
{    
    if (action == @selector(copy:) ||
        action == @selector(paste:)||
        action == @selector(cut:)) 
    {
        return _copyCutAndPasteEnabled;
    }
    return [super canPerformAction:action withSender:sender];
}
于 2011-05-18T21:19:46.960 に答える
105

iOS 5.0~8.0で以下のコードが動作することを確認しております。

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

iOS 9 以降でも動作します。迅速なコードは次のとおりです。

func webViewDidFinishLoad(webView: UIWebView) {
    // Disable user selection
    webView.stringByEvaluatingJavaScriptFromString("document.documentElement.style.webkitUserSelect='none'")!
    // Disable callout
    webView.stringByEvaluatingJavaScriptFromString("document.documentElement.style.webkitTouchCallout='none'")!
}
于 2012-10-02T16:29:15.487 に答える
26

この手法をAndroid/iPhone用のWebアプリ(Trigger.IOにパッケージ化されている)で使用していますが、:not()疑似クラスの連鎖構文でのみ機能することがわかりました。

*: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 */

}
于 2013-01-20T12:44:54.967 に答える
18

私は WrightsCS ソリューションが気に入っていますが、ユーザーが入力に対してコピー、貼り付け、および選択アクションを引き続き使用できるように、これを使用します

<style type="text/css">
*:not(input,textarea) {
    -webkit-touch-callout: none;
    -webkit-user-select: none; /* Disable selection/Copy of UIWebView */
}
</style>
于 2012-11-02T20:58:59.840 に答える
9

セットアップがどのように行われるのかわかりませんが、viewWillDisappear が呼び出されたときに pasteBoard をクリアしないでください。多分あなたの appDelegate.m のようなもの:

[UIPasteboard generalPasteboard].string = nil;

これにより、ユーザーがコピーした可能性のあるデータが何であれ、アプリの外に貼り付けることができなくなります。

また、Engin が言ったように、uiwebview を含むコントローラー クラスの canPerformSelector メソッドをオーバーライドできます。

于 2011-05-24T20:45:08.400 に答える
7

TPoschel の回答は正しいですが、私の場合は順序が重要でした。

// this works - locks selection and callout
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];
    [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"];
}

// this doesn't work - locks only callout
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"];
    [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];
}
于 2014-07-29T12:14:55.050 に答える
7

これが間違いなくあなたに役立つことを確認できます。

<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>

アンカー ボタン タグのみを無効にする場合は、これを使用します。

    a {-webkit-user-select: none; /* disable selection/Copy of UIWebView */
   -webkit-touch-callout: none; /* disable the IOS popup when long-press on a link */
     }
于 2014-08-21T07:35:25.810 に答える
5

1週間頑張った成果!多くのページでマウスイベントとユーザー入力を保存する場合、他のすべての回答は正しくありません。

1) スウィズル法 ( rentzsch/jrswizzleライブラリによる):

[NSClassFromString(@"UIWebDocumentView") jr_swizzleMethod:@selector(canPerformAction:withSender:) withMethod:@selector(myCanPerformAction:withSender:) error:nil];

NSObject+myCanPerformAction.h:

@interface NSObject (myCanPerformAction)

- (BOOL)myCanPerformAction:(SEL)action withSender:(id)sender;

@end

NSObject+myCanPerformAction.m:

#import "NSObject+myCanPerformAction.h"

@implementation NSObject (myCanPerformAction)

- (BOOL)myCanPerformAction:(SEL)action withSender:(id)sender {
    if (action == @selector(copy:)) {
        return [self myCanPerformAction:action withSender:sender];
    }
    if (action == @selector(paste:)) {
        return [self myCanPerformAction:action withSender:sender];
    }
    return NO;
}

@end

2) UIView に UIWebView を配置し、コードを追加します。

    UITapGestureRecognizer* singleTap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)] autorelease];
    singleTap.numberOfTapsRequired = 2;
    singleTap.numberOfTouchesRequired = 1;
    singleTap.delegate = self;
    [self.view addGestureRecognizer:singleTap];

そしてこれ:

- (void)handleSingleTap:(UIGestureRecognizer*)gestureRecognizer {
    return;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    if ([otherGestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
        UITapGestureRecognizer *gesture = (UITapGestureRecognizer *)otherGestureRecognizer;
        if (gesture.numberOfTapsRequired == 2) {
            [otherGestureRecognizer.view removeGestureRecognizer:otherGestureRecognizer];
        }
    }
    return YES;
}
于 2012-11-09T00:50:00.380 に答える
4

与えられた最初の解決策は私にとって完璧に機能しました... .pdf を UIWebView にロードするまで。

.doc ファイルの読み込みは完全に機能しましたが、.pdf を読み込むと、次のコード行で目的の効果が得られなくなり、ユーザーが長時間タッチするとコピー/定義メニューが再びポップアップしました。

    [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];

もう一度髪を引っ張った後、ジョニー・ロケックスがここでこの答えを見つけました。それはチャンピオンのように機能しました. PDFファイルを表示するときのコピー/貼り付けなしのUIWebView

この簡単に実装できる天才的なソリューションを提供してくれた彼に感謝します!!

于 2014-01-01T15:39:27.007 に答える
2

私にとっては、NSDataから画像を取得するつもりでしUIWebViewLongPressGesture

しかし、拡大鏡とコピー/貼り付け/切り取りは、関数が実行される前に常に発生します。

そして、私はこれを見つけました: ここに画像の説明を入力

つまり、拡大鏡とコピー/貼り付け/切り取りの実行には 0.5 秒かかるため、関数を 0.49 秒で実行できる場合は完了です。

self.longPressPan.minimumPressDuration = 0.3
于 2015-09-12T06:26:52.867 に答える