Search code examples
swifturluinavigationcontrollerwkwebview

Why not able to navigate from WKWebView delegate method "didFinish" in swift


I am showing CCavenue Url in web page and my all WKWebView delegate methods are called but the thing is I'm unable to navigate to another viewcontroller from didFinish method

code: here am presenting CCAvenuePaymentWebviewVC like this

    let vc = Helper.getVcObject(vcName: .CCAvenuePaymentWebviewVC, storyboardName: .Miscellaneous) as! CCAvenuePaymentWebviewVC
            
     vc.studentId = myId
     vc.modalPresentationStyle =  .pageSheet
     self.present(vc, animated: false, completion: nil)

and here is the code for CCAvenuePaymentWebviewVC when i print then i got print also but unable to go to MyFollowersViewController why? where am i wrong. please guide me

inside the condition

import UIKit
import WebKit
class CCAvenuePaymentWebviewVC: UIViewController, WKNavigationDelegate {

@IBOutlet weak var webView: WKWebView!

override func viewDidLoad() {
    super.viewDidLoad()
    webView.navigationDelegate = self
    loadRequestOnWebView()
}

private func loadRequestOnWebView(){
    
    var apiUrl = "https://project.in/dev/api/student/workshop-payment-initiate-new?workshop_id="
    
    let myURL = URL(string: apiUrl)
    var myRequest = URLRequest(url: myURL!)
    myRequest.httpMethod = "post"
    DispatchQueue.main.async {
        self.webView.load(myRequest)
    }
}

@IBAction func backNavigationButton(_ sender: UIButton) {
    self.dismiss(animated: true, completion: nil)
}

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    print("finish to load")
    
    if let urlAsString = webView.url?.absoluteString{
        let redirectURL = "https://k12studio.in/dev/payment-success"
        
        if urlAsString.contains(redirectURL)  {
            
            print("inside the condition")
            let vc = Helper.getVcObject(vcName: .MyFollowersViewController, storyboardName: .Miscellaneous) as! MyFollowersViewController
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
}
}

Solution

  • You cannot push a ViewController over a presented ViewController. What you need is to attach a UINavigationController to your CCAvenuePaymentWebviewVC

    Try this:

    let vc = Helper.getVcObject(vcName: .CCAvenuePaymentWebviewVC, storyboardName: .Miscellaneous) as! CCAvenuePaymentWebviewVC
    vc.studentId = myId
    let navController = UINavigationController(rootViewController: vc)
    navController.modalPresentationStyle = .fullScreen
    self.present(navController, animated: true, completion: nil)