为什么无法从WKWebView代理方法“didFinish”中导航到Swift?

huangapple go评论77阅读模式
英文:

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

问题

我在网页中显示了CCavenue网址,而且我的所有WKWebView委托方法都被调用了,但问题是我无法从didFinish方法中导航到另一个视图控制器。

**代码:**我像这样呈现CCAvenuePaymentWebviewVC

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

这里是CCAvenuePaymentWebviewVC的代码,当我打印时,我能够打印出来,但无法进入MyFollowersViewController,为什么?我哪里错了,请指导我。

在条件内部:

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)
        }
    }
}
}

希望对你有所帮助。

英文:

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)
        }
    }
}
}

答案1

得分: 0

你不能在已呈现的视图控制器上叠加另一个视图控制器。您需要将一个UINavigationController附加到您的CCAvenuePaymentWebviewVC

尝试这样做:

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)
英文:

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)

huangapple
  • 本文由 发表于 2023年7月3日 17:40:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76603559.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定