简介
EasyWebView可以使应用在UIViewController或UITableViewCell中轻松使用WKWebView。
功能
- 一行代码即可通过加载URL或HTML字符串来使用WKWebView。
- 智能且易于调整UITableViewCell中WebView高度。
- 易于使用并安全。
- 支持常见的WKWebView功能。
安装
要求
- Swift 4.2
- iOS 9.0
Cocoapods
EasyWebView可通过CocoaPods获得。要安装它,只需将以下行添加到您的Podfile文件中:
platform :ios, '9.0'
pod "EasyWebView"
如果想要加载http URL,请务必在
info.plist
中添加隐私描述。
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
使用方法
使用 WebViewController
class ViewController: UIViewController {
func pushToWebViewController() {
pushToWebByLoadingURL("google.com", title: "Google")
}
}
重写 WebViewController
class CustomWebViewController: WebViewController {
override var isShowProgressView: Bool { return false }
override var isShowTitle: Bool { return false }
}
使用 WebViewCell
class ViewController: UITableViewController {
private var webCellHeight: CGFloat = 300
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(WebViewCell.self, forCellReuseIdentifier: String(describing: WebViewCell.self))
}
}
extension ViewController {
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return webCellHeight
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withClass: WebViewCell.self, for: indexPath)
// test url and scriptNames
cell.setupURLString("https://test.com", delegate: self, isAddObservers: true)
cell.webView.addScriptMessageHandler(scriptNames: ["test"]) { (_, message) in
print("😄: \(message.body)")
}
return cell
}
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
tableView.fixWebViewCellRenderingWhite()
}
}
作者
您的组织或项目是否使用 EasyWebView?请通过电子邮件告知我。John,[email protected]。
许可证
EasyWebView 在 MIT 许可证下可用。