Copy 1- import WebKit
2- Implement WKUIDelegate ,WKScriptMessageHandler
3- Connect with UI
@IBOutlet weak var webViewContainer: UIView!
4- Define variables
var webView: WKWebView!
var transparentWebView = false
5- First Method :
override func viewWillAppear(_ animated: Bool) {
if transparentWebView == true {
self.webViewTransparent()
}
}
6- Handle Transparent case
func webViewTransparent() {
webView.backgroundColor = .clear
webView.isOpaque = false
view.backgroundColor = .clear
}
7- Then you can make configuration for webView
fileprivate func setupUI() {
self.title = ""
let contentController = WKUserContentController()
contentController.add(
self,
name: "Mobile"
)
let config = WKWebViewConfiguration()
config.userContentController = contentController
webView = WKWebView(frame: webViewContainer.bounds, configuration: config)
webView.translatesAutoresizingMaskIntoConstraints = false
webViewContainer.addSubview(webView)
webV1- import WebKit
2- Implement WKUIDelegate ,WKScriptMessageHandler
3- Connect with UI
@IBOutlet weak var webViewContainer: UIView!
4- Define variables
var webView: WKWebView!
var transparentWebView = false
5- First Method :
override func viewWillAppear(_ animated: Bool) {
if transparentWebView == true {
self.webViewTransparent()
}
}
6- Handle Transparent case
func webViewTransparent() {
webView.backgroundColor = .clear
webView.isOpaque = false
view.backgroundColor = .clear
}
7- Then you can make configuration for webView
fileprivate func setupUI() {
self.title = ""
let contentController = WKUserContentController()
contentController.add(
self,
name: "Mobile"
)
let config = WKWebViewConfiguration()
config.userContentController = contentController
webView = WKWebView(frame: webViewContainer.bounds, configuration: config)
webView.translatesAutoresizingMaskIntoConstraints = false
webViewContainer.addSubview(webView)
webView.leadingAnchor.constraint(equalTo: webViewContainer.leadingAnchor, constant: 0).isActive = true
webView.trailingAnchor.constraint(equalTo: webViewContainer.trailingAnchor, constant: 0).isActive = true
webView.topAnchor.constraint(equalTo: webViewContainer.topAnchor, constant: 0).isActive = true
webView.bottomAnchor.constraint(equalTo: webViewContainer.bottomAnchor, constant: 0).isActive = true
webView.navigationDelegate = self
webView.uiDelegate = self
if let urlPath = URL(string: url) {
webView.load(URLRequest(url: urlPath))
}
}
8- You can call this message to handle response
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
getMessageData(message: message)
}
9- Then handle getMessage method
func getMessageData(message: WKScriptMessage) {
guard let data = message.body as? NSDictionary else {return}
if let methodName = data["method_name"] as? String {
switch methodName {
case "closeWindow":
navigationController?.popViewController(animated: true)
dismiss(animated: true, completion: nil)
case "openChargePage":
let view = WalletRouter.createModule()
navigationController?.pushViewController(view, animated: true)
case "backgroundTransparent":
hideNavigationBar()
webViewTransparent()
}
}
}iew.leadingAnchor.constraint(equalTo: webViewContainer.leadingAnchor, constant: 0).isActive = true
webView.trailingAnchor.constraint(equalTo: webViewContainer.trailingAnchor, constant: 0).isActive = true
webView.topAnchor.constraint(equalTo: webViewContainer.topAnchor, constant: 0).isActive = true
webView.bottomAnchor.constraint(equalTo: webViewContainer.bottomAnchor, constant: 0).isActive = true
webView.navigationDelegate = self
webView.uiDelegate = self
if let urlPath = URL(string: url) {
webView.load(URLRequest(url: urlPath))
}
}
8- You can call this message to handle response
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
getMessageData(message: message)
}
9- Then handle getMessage method
func getMessageData(message: WKScriptMessage) {
guard let data = message.body as? NSDictionary else {return}
if let methodName = data["method_name"] as? String {
switch methodName {
case "closeWindow":
navigationController?.popViewController(animated: true)
dismiss(animated: true, completion: nil)
case "openChargePage":
let view = WalletRouter.createModule()
navigationController?.pushViewController(view, animated: true)
case "backgroundTransparent":
hideNavigationBar()
webViewTransparent()
}
}
}