如何禁用UIWebView的复制和剪切

时间:2018-09-20 19:06:13

标签: ios objective-c xcode uiwebview

我按照此答案(https://stackoverflow.com/a/6051172/8710951)禁用了UIWebView的复制和剪切。

我是通过扩展UIWebView的类别来实现的。

UIWebView + DisableCopy.h

#import <UIKit/UIKit.h>

@interface UIWebView (DisableCopy)

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

@end

UIWebView + DisableCopy.m

#import "UIWebView+DisableCopy.h"

@implementation UIWebView (DisableCopy)

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    BOOL superCanPerform = [super canPerformAction:action withSender:sender];

    NSLog([NSString stringWithFormat:@"Current action: [%@]", NSStringFromSelector(action)]);

    if (superCanPerform) {
        if (action == @selector(copy:) ||
            action == @selector(paste:)||
            action == @selector(cut:))
        {
            return NO;
        }
    }
    return superCanPerform;
}

@end

但是,似乎copy, paste, and cut操作没有传递给canPerformAction。我记录了通过的所有操作:

11:57:00.996578-0700 WebViewPlayground[69631:6602598] Current action: [select:]
11:57:00.997104-0700 WebViewPlayground[69631:6602598] Current action: [selectAll:]
11:57:01.029183-0700 WebViewPlayground[69631:6602598] Current action: [delete:]
11:57:01.030782-0700 WebViewPlayground[69631:6602598] Current action: [_transliterateChinese:]
11:57:01.030933-0700 WebViewPlayground[69631:6602598] Current action: [_insertDrawing:]
11:57:01.031117-0700 WebViewPlayground[69631:6602598] Current action: [_showTextStyleOptions:]
11:57:01.031276-0700 WebViewPlayground[69631:6602598] Current action: [_lookup:]
11:57:01.032307-0700 WebViewPlayground[69631:6602598] Current action: [_addShortcut:]
11:57:01.032707-0700 WebViewPlayground[69631:6602598] Current action: [_accessibilitySpeak:]
11:57:01.032872-0700 WebViewPlayground[69631:6602598] Current action: [_accessibilitySpeakLanguageSelection:]
11:57:01.033054-0700 WebViewPlayground[69631:6602598] Current action: [_accessibilityPauseSpeaking:]
11:57:01.033754-0700 WebViewPlayground[69631:6602598] Current action: [makeTextWritingDirectionRightToLeft:]
11:57:01.033922-0700 WebViewPlayground[69631:6602598] Current action: [makeTextWritingDirectionLeftToRight:]

0 个答案:

没有答案