美文网首页
WKWebView长按弹出框UIMenuController添加

WKWebView长按弹出框UIMenuController添加

作者: 毛豆爱吃奶 | 来源:发表于2020-05-29 22:57 被阅读0次

    项目中是在tableviewcell上加载wkwebview,长按弹出自定义菜单功能。废话不多说直接上代码,全是干货。

    - (instancetype)initWithStyle:(UITableViewCellStyle)stylereuseIdentifier:(NSString*)reuseIdentifier

    {

        if(self= [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

            [self becomeFirstResponder]; //必须添加,如果不添加的话,长按wkwebview系统自带的菜单仍显示

            [self createWebView];

        }

        return self;

    }

    #pragma mark- webView

    - (void)createWebView

    {

        WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];

        [wkWebConfig.userContentController addScriptMessageHandler:self name:@"YSPnativeCmd"];

        UIMenuController * menu = [UIMenuController sharedMenuController];

        UIMenuItem*item0 = [[UIMenuItemalloc]initWithTitle:@"做笔记"action:@selector(TakenotesDidClicked:)];

        UIMenuItem*item2 = [[UIMenuItemalloc]initWithTitle:@"分享图片"action:@selector(ShareImageDidCicked:)];

        [menusetMenuItems:@[item0,item2]];

        self.WKHeadWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 5, WXScreenWidth , 1) configuration:wkWebConfig];

        self.WKHeadWebView.backgroundColor = [UIColor clearColor];

        self.WKHeadWebView.opaque = NO;

        self.WKHeadWebView.userInteractionEnabled = YES;

        self.WKHeadWebView.scrollView.bounces = NO;

        self.WKHeadWebView.UIDelegate = self;

        self.WKHeadWebView.navigationDelegate = self;

        self.WKHeadWebView.scrollView.scrollEnabled = NO;

        self.WKHeadWebView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

        [self.WKHeadWebView sizeToFit];

        [self.WKHeadWebView.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];

        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WXScreenWidth, 1)];

        self.scrollView.scrollEnabled = NO;

        [self.scrollView addSubview:self.WKHeadWebView];

        [self.contentView addSubview:self.scrollView];

    }

    #pragma mark --menutViewController

    /**

     * 通过这个方法告诉UIMenuController它内部应该显示什么内容

     * 返回YES,就代表支持action这个操作

     */

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

        if(action ==@selector(TakenotesDidClicked:) || action ==@selector(ShareImageDidCicked:)) {

            return YES; // YES ->  代表我们只监听分享图片: / 做笔记:方法

        }

        return NO; // 除了上面的操作,都不支持

    }

    // 说明控制器可以成为第一响应者

    - (BOOL)canBecomeFirstResponder {

        return YES;

    }

    //在tableviewcell上加载wkwebview必须添加该方法,如果不添加的话,长按wkwebview系统自带的菜单仍显示

    - (BOOL)canResignFirstResponder {

        return NO;

    }

    //分享图片

    - (void)ShareImageDidCicked:(UIMenuController *)menu

    {

    [self.WKHeadWebView evaluateJavaScript:@"window.getSelection().toString()" completionHandler:^(id _Nullable content, NSError * _Nullable error) {

                NSString*selectContent = (NSString*)content;

            }];

    }

    //做笔记

    - (void)TakenotesDidClicked:(UIMenuController *)menu

    {

    [self.WKHeadWebView evaluateJavaScript:@"window.getSelection().toString()" completionHandler:^(id _Nullable content, NSError * _Nullable error) {

                NSString*selectContent = (NSString*)content;

            }];

    }

    相关文章

      网友评论

          本文标题:WKWebView长按弹出框UIMenuController添加

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