flutter ios webview自定义长按菜单

webview自定义长按菜单,保留选择,粘贴删除功能。

enableCustomMenu添加到初始化位置。 

func enableCustomMenu() {
    let lookup = UIMenuItem(title: "分享", action: #selector(customShare))
    UIMenuController.shared.menuItems = [lookup]
    UIMenuController.shared.update()
}

public override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
     if #available(iOS 10, *) {
        if action == #selector(UIResponderStandardEditActions.select(_:))
            || action == #selector(UIResponderStandardEditActions.cut(_:))
            || action == #selector(UIResponderStandardEditActions.paste(_:))
            || action == #selector(UIResponderStandardEditActions.delete(_:))
            || (action == #selector(customShare))
        {
            return super.canPerformAction(action, withSender:sender)
        }
    } else {
        if  action == #selector(select(_:))
           || action == #selector(cut(_:))
           || action == #selector(paste(_:))
           || action == #selector(delete(_:))
           || (action == #selector(customShare))
        {
            return super.canPerformAction(action, withSender:sender)
        }
    }
    return false
}

@objc func customShare() {
    self.evaluateJavaScript("window.getSelection().toString();") { (result, error)       
      in
        if error == nil {
            let url = self.currentURL?.absoluteString
            let arguments: [String: Any] = ["url": url, "text": result as Any]
            self.channel?.invokeMethod("onSelectText", arguments: arguments)
        }
    }
}

 

你可能感兴趣的:(flutter ios webview自定义长按菜单)