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

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Convert NsNumber, NSDate to String in iOS Swift

Global LocationManager Singleton class iOS Swift