美文网首页
禁止长按WKWebView出现菜单

禁止长按WKWebView出现菜单

作者: SJon | 来源:发表于2016-12-19 12:16 被阅读1582次
        // 禁止选择CSS
        NSString *css = @"body{-webkit-user-select:none;-webkit-user-drag:none;}";
        
        // CSS选中样式取消
        NSMutableString *javascript = [NSMutableString string];
        [javascript appendString:@"var style = document.createElement('style');"];
        [javascript appendString:@"style.type = 'text/css';"];
        [javascript appendFormat:@"var cssContent = document.createTextNode('%@');", css];
        [javascript appendString:@"style.appendChild(cssContent);"];
        [javascript appendString:@"document.body.appendChild(style);"];
        
        // javascript注入
        WKUserScript *noneSelectScript = [[WKUserScript alloc] initWithSource:javascript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
        WKUserContentController *userContentController = [[WKUserContentController alloc] init];
        [userContentController addUserScript:noneSelectScript];
        WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
        configuration.userContentController = userContentController;
        
        
        
        CGRect frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - 44.f -20.f);
    
        WKWebView *webView = [[WKWebView alloc] initWithFrame:frame configuration:configuration];
        
        NSURLRequest* request = [NSURLRequest requestWithURL:_brandInfoURL];
        
        [webView loadRequest:request];
        
        _webView = webView;
        
        [self.view addSubview:webView];
    

    相关文章

      网友评论

          本文标题:禁止长按WKWebView出现菜单

          本文链接:https://www.haomeiwen.com/subject/iwqtvttx.html