Preview the url data in viewcontroller iOS swift
Solution:
The below code will preview the url content using WKWebView.Just create a view controller in storyboard and add wkwebview then connect to previewWeb.Also connect close button.It will works well.
Just call the PreviewVC with assign url.
import UIKit
import RxSwift
import RxCocoa
import WebKit
class PreviewVC: 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