OC写法:
// 禁止选择CSS
NSString *cssStr = @"body{-webkit-user-select:none;-webkit-user-drag:none;}";
// CSS选中样式取消
NSMutableString *jsStr = [NSMutableString stringWithString:@"var style = document.createElement('style');"];
[jsStr appendString:@"style.type = 'text/css';"];
[jsStr appendString:[NSString stringWithFormat:@"var cssContent = document.createTextNode('%@');", cssStr]];
[jsStr appendString:@"style.appendChild(cssContent);"];
[jsStr appendString:@"document.body.appendChild(style);"];
// 禁止选择
[jsStr appendString:@"document.documentElement.style.webkitUserSelect='none';"];
// 禁止长按
[jsStr appendString:@"document.documentElement.style.webkitTouchCallout='none';"];
WKUserScript *userScript = [[WKUserScript alloc] initWithSource:jsStr injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
WKUserContentController *userContentController = [[WKUserContentController alloc] init];
[userContentController addUserScript:userScript];
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
config.userContentController = userContentController;
// 初始化WKWebView
WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:config];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.fileUrl]]];
[self.view addSubview:webView];
WKWebView禁止UIMenuController在移动应用的开发中,不可避免的会使用到HTML页面,在iOS的开发中,用来展示网页的一般是UIWebView,在iOS 8.0之后,Apple提供了一个新的框架(WKWebV...