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

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

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

问题

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

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

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

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

在条件内部:

  1. import UIKit
  2. import WebKit
  3. class CCAvenuePaymentWebviewVC: UIViewController, WKNavigationDelegate {
  4. @IBOutlet weak var webView: WKWebView!
  5. override func viewDidLoad() {
  6. super.viewDidLoad()
  7. webView.navigationDelegate = self
  8. loadRequestOnWebView()
  9. }
  10. private func loadRequestOnWebView(){
  11. var apiUrl = "https://project.in/dev/api/student/workshop-payment-initiate-new?workshop_id="
  12. let myURL = URL(string: apiUrl)
  13. var myRequest = URLRequest(url: myURL!)
  14. myRequest.httpMethod = "post"
  15. DispatchQueue.main.async {
  16. self.webView.load(myRequest)
  17. }
  18. }
  19. @IBAction func backNavigationButton(_ sender: UIButton) {
  20. self.dismiss(animated: true, completion: nil)
  21. }
  22. func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  23. print("finish to load")
  24. if let urlAsString = webView.url?.absoluteString{
  25. let redirectURL = "https://k12studio.in/dev/payment-success"
  26. if urlAsString.contains(redirectURL) {
  27. print("inside the condition")
  28. let vc = Helper.getVcObject(vcName: .MyFollowersViewController, storyboardName: .Miscellaneous) as! MyFollowersViewController
  29. self.navigationController?.pushViewController(vc, animated: true)
  30. }
  31. }
  32. }
  33. }

希望对你有所帮助。

英文:

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

  1. let vc = Helper.getVcObject(vcName: .CCAvenuePaymentWebviewVC, storyboardName: .Miscellaneous) as! CCAvenuePaymentWebviewVC
  2. vc.studentId = myId
  3. vc.modalPresentationStyle = .pageSheet
  4. 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

  1. import UIKit
  2. import WebKit
  3. class CCAvenuePaymentWebviewVC: UIViewController, WKNavigationDelegate {
  4. @IBOutlet weak var webView: WKWebView!
  5. override func viewDidLoad() {
  6. super.viewDidLoad()
  7. webView.navigationDelegate = self
  8. loadRequestOnWebView()
  9. }
  10. private func loadRequestOnWebView(){
  11. var apiUrl = "https://project.in/dev/api/student/workshop-payment-initiate-new?workshop_id="
  12. let myURL = URL(string: apiUrl)
  13. var myRequest = URLRequest(url: myURL!)
  14. myRequest.httpMethod = "post"
  15. DispatchQueue.main.async {
  16. self.webView.load(myRequest)
  17. }
  18. }
  19. @IBAction func backNavigationButton(_ sender: UIButton) {
  20. self.dismiss(animated: true, completion: nil)
  21. }
  22. func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  23. print("finish to load")
  24. if let urlAsString = webView.url?.absoluteString{
  25. let redirectURL = "https://k12studio.in/dev/payment-success"
  26. if urlAsString.contains(redirectURL) {
  27. print("inside the condition")
  28. let vc = Helper.getVcObject(vcName: .MyFollowersViewController, storyboardName: .Miscellaneous) as! MyFollowersViewController
  29. self.navigationController?.pushViewController(vc, animated: true)
  30. }
  31. }
  32. }
  33. }

答案1

得分: 0

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

尝试这样做:

  1. let vc = Helper.getVcObject(vcName: .CCAvenuePaymentWebviewVC, storyboardName: .Miscellaneous) as! CCAvenuePaymentWebviewVC
  2. vc.studentId = myId
  3. let navController = UINavigationController(rootViewController: vc)
  4. navController.modalPresentationStyle = .fullScreen
  5. 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:

  1. let vc = Helper.getVcObject(vcName: .CCAvenuePaymentWebviewVC, storyboardName: .Miscellaneous) as! CCAvenuePaymentWebviewVC
  2. vc.studentId = myId
  3. let navController = UINavigationController(rootViewController: vc)
  4. navController.modalPresentationStyle = .fullScreen
  5. 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:

确定