OC寫(xiě)法:
// 禁止選擇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';"];
// 禁止長(zhǎng)按
[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在移動(dòng)應(yīng)用的開(kāi)發(fā)中,不可避免的會(huì)使用到HTML頁(yè)面,在iOS的開(kāi)發(fā)中,用來(lái)展示網(wǎng)頁(yè)的一般是UIWebView,在iOS 8.0之后,Apple提供了一個(gè)新的框架(WKWebV...