Hello everyone, I am developing a very simple app which contains only 2 view controllers. First one for app introduction (I have used Collection view for sliding images) and second one for loading website in wkwebview. Issue is that the website is a little bit heavy and takes around 5-8 seconds to load.
I need that that wekwebview to be loaded in the first view controller and once it is loaded it automatically pushes to the second view controller in which wkwebview is.
Following is what I have done till now
First view controller
class OnBoardingViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var pageControl: UIPageControl!
@IBOutlet weak var LoginBtn: UIButton!
@IBOutlet weak var SignupBtn: UIButton!
var slides: [OnboardingSlide] = []
var currenrPage = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
slides = [OnboardingSlide(image: UIImage(imageLiteralResourceName: "slide1")),
OnboardingSlide(image: UIImage(imageLiteralResourceName: "slide2")),
OnboardingSlide(image: UIImage(imageLiteralResourceName: "slide3"))]
let wvc = storyboard!.instantiateViewController(withIdentifier: "webViewController") as! ViewController
//wvc.modalPresentationStyle = .fullScreen
wvc.loadWebView {
self.navigationController?.pushViewController(wvc, animated: true)
//self.present(wvc, animated: true, completion: nil)
}
}
Second view controller (wkwebview is in this view controller)
import UIKit
import WebKit
class ViewController: UIViewController {
@IBOutlet weak var myWebView: WKWebView!
var didLoadWebView: (() -> Void)!
func loadWebView(completion: @escaping () -> Void){
loadViewIfNeeded()
didLoadWebView = completion
myWebView.navigationDelegate = self
guard let url = URL(string: "https://fashionhind.com") else {return}
myWebView.load(URLRequest(url: url))
myWebView.allowsBackForwardNavigationGestures = true
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.navigationController?.isNavigationBarHidden = true
}
override func viewDidAppear(_ animated: Bool) {
self.navigationController?.isNavigationBarHidden = true
}
}
extension ViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
didLoadWebView() // Call back to the presenting view controller
}
}
Following is the issue Whenever I run the wkwebview loads fine but when I click on any button on the website app crashed with following error in this line "self.navigationController?.pushViewController(wvc, animated: true)"
"Exception NSException * "Application tried to present modally a view controller <Fashionhind.ViewController: 0x14bf18ed0> that is already being presented by <UINavigationController: 0x14d81aa00>." 0x0000600001b2d320"
Thanks in advance for help