页面禁用长按事件
方式一
禁用长按选择
代码语言:javascript复制func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
//页面加载完成时
webView.evaluateJavaScript(
"document.documentElement.style.webkitUserSelect='none';",
completionHandler: nil
);
webView.evaluateJavaScript(
"document.documentElement.style.webkitTouchCallout='none';",
completionHandler: nil
)
}
方式二
完全禁用长按事件
代码语言:javascript复制func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
//页面加载完成时
for subview in webView.scrollView.subviews {
if(subview.gestureRecognizers != nil){
for longPress in subview.gestureRecognizers!{
if(longPress is UILongPressGestureRecognizer){
subview.removeGestureRecognizer(longPress)
}
}
}
}
}
常用代理方法
- WKUIDelegate
- WKNavigationDelegate
- WKScriptMessageHandler
lazy var webConfiguration: WKWebViewConfiguration = {
let configuration = WKWebViewConfiguration.init()
let preferences = WKPreferences.init()
preferences.javaScriptCanOpenWindowsAutomatically = true
configuration.preferences = preferences
configuration.userContentController = self.webUserContentController
return configuration
}()
lazy var webUserContentController: WKUserContentController = {
let userConetentController = WKUserContentController.init()
userConetentController.add(self, name: "webViewApp")
return userConetentController
}()
func setupWebView() {
myWebView = WKWebView.init(frame: self.webouterView.bounds, configuration: webConfiguration)
self.webouterView.addSubview(myWebView)
myWebView.autoresizingMask = UIView.AutoresizingMask.flexibleWidth
let web_url = URL.init(string: weburl)
myWebView.load(URLRequest.init(url: web_url!))
myWebView.navigationDelegate = self
myWebView.uiDelegate = self
myWebView.scrollView.bounces = false;
}
override func willAnimateRotation(to toInterfaceOrientation: UIInterfaceOrientation, duration: TimeInterval) {
myWebView.frame = self.webouterView.bounds
}
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
if(!(navigationAction.targetFrame?.isMainFrame ?? true)){
webView.load(navigationAction.request)
}
return nil
}
//MARK:-WKUIDelegate
func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
// 在JS端调用alert函数时,会触发此代理方法。
// JS端调用alert时所传的数据可以通过message拿到
// 在原生得到结果后,需要回调JS,是通过completionHandler回调
self.showNoticeText(message, time: 1.2) {
completionHandler()
}
}
func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void) {
// JS端调用confirm函数时,会触发此方法
// 通过message可以拿到JS端所传的数据
// 在iOS端显示原生alert得到YES/NO后
// 通过completionHandler回调给JS端
let alertView = UIAlertController.init(title: "提示", message:message, preferredStyle: UIAlertController.Style.alert)
let cancelAction = UIAlertAction.init(title: "取消", style: UIAlertAction.Style.cancel) {
(action:UIAlertAction) in
//取消
completionHandler(false)
}
alertView.addAction(cancelAction)
let okAction = UIAlertAction.init(title: "确定", style: UIAlertAction.Style.default) {
(action:UIAlertAction) in
//确定
completionHandler(true)
}
alertView.addAction(okAction)
self.present(alertView, animated: true, completion: nil)
}
func webView(_ webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String, defaultText: String?, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (String?) -> Void) {
// JS端调用prompt函数时,会触发此方法
// 要求输入一段文本
// 在原生输入得到文本内容后,通过completionHandler回调给JS
let alertTextField = UIAlertController.init(title: "请输入", message: "JS调用输入框", preferredStyle: UIAlertController.Style.alert)
alertTextField.addTextField {
(textField:UITextField) in
//设置textField相关属性
textField.textColor = UIColor.red
}
let okAction = UIAlertAction.init(title: "确定", style: UIAlertAction.Style.destructive) { (action:UIAlertAction) in
//确定
completionHandler(alertTextField.textFields?.last?.text)
}
alertTextField.addAction(okAction)
self.present(alertTextField, animated: true, completion: nil)
}
//MARK:-WKNavigationDelegate
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
//页面开始加载,可在这里给用户loading提示
self.showNoticeWait(text: "加载中...");
}
func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
//内容开始到达时
self.clearAllNotice();
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
//页面加载完成时
for subview in webView.scrollView.subviews {
if(subview.gestureRecognizers != nil){
for longPress in subview.gestureRecognizers!{
if(longPress is UILongPressGestureRecognizer){
subview.removeGestureRecognizer(longPress)
}
}
}
}
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
//页面加载出错,可在这里给用户错误提示
}
func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
//收到服务器重定向请求
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
// 在请求开始加载之前调用,决定是否跳转
decisionHandler(WKNavigationActionPolicy.allow)
}
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
//在收到响应开始加载后,决定是否跳转
decisionHandler(WKNavigationResponsePolicy.allow)
}
//MARK:-WKScriptMessageHandler
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
//h5给端传值的内容,可在这里实现h5与原生的交互时间
let messageDic = message.body
print(messageDic)
}