英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论