import UIKit
import WebKit
class MainTableVC: UIViewController {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.separatorStyle = .none
self.tableView.showsHorizontalScrollIndicator = false
// 커스텀 셀을 가져오는 부분
self.tableView.register(UINib(nibName: "Cell", bundle: nil), forCellReuseIdentifier: "")
}
}
extension MainTableVC: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "셀 identifier") as! TableViewCell
let registerCellView = cell.setWebView(rootVC: self, frame: self.view.frame)
registerCellView.uiDelegate = self
registerCellView.navigationDelegate = self
cell.addSubview(registerCellView)
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
}
extension MainTableVC: WKUIDelegate, WKNavigationDelegate {
//모달창 닫힐때 앱 종료현상 방지
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// alert
func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
let alertController = UIAlertController(title: "", message: message, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "확인", style: .default, handler: {(action) in completionHandler()}))
self.present(alertController, animated: true, completion: nil)
}
// confirm
func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void) {
let alertController = UIAlertController(title: "", message: message, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "확인", style: .default, handler: {(action) in completionHandler(true)}))
alertController.addAction((UIAlertAction(title: "취소", style: .default, handler: { (action) in completionHandler(false)})))
self.present(alertController, animated: true, completion: nil)
}
// href="_blank" 처리
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
if navigationAction.targetFrame == nil {
webView.load(navigationAction.request)
}
return nil
}
}