美文网首页
iOS中 WKWebview有效实现禁止滑动,禁止双指缩放,禁止

iOS中 WKWebview有效实现禁止滑动,禁止双指缩放,禁止

作者: 轻重缓急 | 来源:发表于2019-07-17 21:04 被阅读0次

    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    config.preferences = [WKPreferences new];
    config.preferences.minimumFontSize = 10;
    config.preferences.javaScriptEnabled = YES; config.preferences.javaScriptCanOpenWindowsAutomatically = YES;

    //下方代码,禁止缩放
    WKUserContentController *userController = [WKUserContentController new];
    NSString *js = @" ('meta[name=description]').remove();('head').append( '<meta name="viewport" content="width=device-width, initial-scale=1,user-scalable=no">' );";
    WKUserScript *script = [[WKUserScript alloc] initWithSource:js injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:NO]; NSMutableString *javascript = [NSMutableString string];
    [javascript appendString:@"document.documentElement.style.webkitTouchCallout='none';"];

    //禁止长按
    [javascript appendString:@"document.documentElement.style.webkitUserSelect='none';"];

    //禁止选择
    WKUserScript *noneSelectScript = [[WKUserScript alloc] initWithSource:javascript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
    [userController addUserScript:script];
    [userController addUserScript:noneSelectScript];
    [userController addScriptMessageHandler:self name:@"openInfo"];
    config.userContentController = userController;
    _webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:config];
    _webView.scrollView.bounces = false;//禁止滑动

    相关文章

      网友评论

          本文标题:iOS中 WKWebview有效实现禁止滑动,禁止双指缩放,禁止

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