你貌似开启了广告屏蔽插件... 写博客不容易,希望能将广告过滤去掉,谢谢!

swift4 WKWebView使用JS与Swift交互

2018-07-05   朋也   swift学习笔记(纯代码)   swift4  

创建wkwebview

import WebKit

lazy var webView: WKWebView = {
    let preferences = WKPreferences()
    preferences.javaScriptEnabled = true

    let configuration = WKWebViewConfiguration()
    configuration.preferences = preferences
    configuration.userContentController = WKUserContentController()
    // 给webview与swift交互起一个名字:AppModel,webview给swift发消息的时候会用到
    configuration.userContentController.add(self, name: "AppModel")

    var webView = WKWebView(frame: self.view.frame, configuration: configuration)
    // 让webview翻动有回弹效果
    webView.scrollView.bounces = true
    // 只允许webview上下滚动
    webView.scrollView.alwaysBounceVertical = true
    webView.navigationDelegate = self
    return webView
}()