WKWebview implementation in iOS swift
Problem:
I want to create a webview that can contain close button as well as webview to load the content.
Solution:
import UIKit
import RxSwift
import RxCocoa
import WebKit
class MyVC: UIViewController, WKUIDelegate, WKNavigationDelegate {
@IBOutlet weak var closeBtn: UIButton!
@IBOutlet weak var previewWeb: WKWebView!
var url:URL?
var disposebag = DisposeBag()
override func viewDidLoad() {
previewWeb.uiDelegate = self
previewWeb.navigationDelegate = self
self.closeBtn.tintColor = .white
self.closeBtn.rx.tap.asDriver()
.drive(onNext: { [weak self] in
self?.dismiss(animated: true)
}).disposed(by: self.disposebag)
if let myUrl = url {
if #available(iOS 16.0, *) {
self.previewWeb.load(URLRequest(url:myUrl))
} else {
// Fallback on earlier versions
self.previewWeb.loadFileURL(myUrl, allowingReadAccessTo: myUrl)
}
}
}
Comments
Post a Comment