Ios 尝试在Swift中每隔120秒添加URL自动重定向

Ios 尝试在Swift中每隔120秒添加URL自动重定向,ios,swift,wkwebview,Ios,Swift,Wkwebview,我是一个非常新的编码,所以任何帮助将不胜感激 我试图收集一些基本的调查数据,这些数据涉及我在工作中测试的视频会议技术,以及我在调查完成后使用的付费解决方案。由于我无法在每次进行调查时重新启动web url,因此我希望该页面每2分钟自动重定向回初始url页面。下面是我现有的代码。 谢谢 第一次可以在viewDidLoad()中调用toDoTask(),您可以尝试以下代码: import UIKit import WebKit class HomeViewController: UIViewCon

我是一个非常新的编码,所以任何帮助将不胜感激

我试图收集一些基本的调查数据,这些数据涉及我在工作中测试的视频会议技术,以及我在调查完成后使用的付费解决方案。由于我无法在每次进行调查时重新启动web url,因此我希望该页面每2分钟自动重定向回初始url页面。下面是我现有的代码。 谢谢

第一次可以在
viewDidLoad()

中调用
toDoTask()
,您可以尝试以下代码:

import UIKit
import WebKit

class HomeViewController: UIViewController, WKNavigationDelegate{

    var webView = WKWebView()

    override func viewDidLoad() {
        super.viewDidLoad()
        webView.navigationDelegate = self
        view = webView
        let url = URL(string: "https://Mysurveypage/1234")!
        webView.load(URLRequest(url: url))
        webView.allowsBackForwardNavigationGestures = true
    }

    override func viewDidAppear(_ animated: Bool) {
        //Uncomment the following line of code to reload the webview currently on top of navigation stack, or in plain words, current browser tab showing in mobile window, Comment the other timer code if you intend to do this
        //Timer.scheduledTimer(timeInterval: 2 * 60, target: self, selector: #selector(reloadWebView), userInfo: nil, repeats: true)

        // Following line of code reloads the initial url i.e. "https://Mysurveypage/1234"
        Timer.scheduledTimer(timeInterval: 2 * 60, target: self, selector: #selector(redirectToInitialUrl), userInfo: nil, repeats: true)
    }

    @objc private func reloadWebView() {
        webView.reload()
    }

    @objc private func redirectToInitialUrl() {
        let url = URL(string: "https://Mysurveypage/1234")!
        webView.load(URLRequest(url: url))
    }
}
PS:Apple文档中说:

永远不要直接调用此方法


通常在viewDidLoad()方法中执行UIViewController生命周期中希望执行的所有一次性函数/操作。

非常感谢您的回复。所以不幸的是,我对编码是如此陌生,我不知道在哪里输入Jagdeep的代码,但是复制naeemjawaid的代码非常有效。我确实必须从类ViewController行中删除单词“Home”。在那之后,一切都很好。再次感谢您的帮助。

OP没有呼叫
loadView
。OP发布的代码没有问题。
var timer = Timer.scheduledTimer(timeInterval: 120.0, target: self, selector: #selector(ViewController.toDoTask), userInfo: nil, repeats: true)



func toDoTask() 
 {
    let url = URL(string: "https://Mysurveypage/1234")!
    webView.load(URLRequest(url: url))



 }
import UIKit
import WebKit

class HomeViewController: UIViewController, WKNavigationDelegate{

    var webView = WKWebView()

    override func viewDidLoad() {
        super.viewDidLoad()
        webView.navigationDelegate = self
        view = webView
        let url = URL(string: "https://Mysurveypage/1234")!
        webView.load(URLRequest(url: url))
        webView.allowsBackForwardNavigationGestures = true
    }

    override func viewDidAppear(_ animated: Bool) {
        //Uncomment the following line of code to reload the webview currently on top of navigation stack, or in plain words, current browser tab showing in mobile window, Comment the other timer code if you intend to do this
        //Timer.scheduledTimer(timeInterval: 2 * 60, target: self, selector: #selector(reloadWebView), userInfo: nil, repeats: true)

        // Following line of code reloads the initial url i.e. "https://Mysurveypage/1234"
        Timer.scheduledTimer(timeInterval: 2 * 60, target: self, selector: #selector(redirectToInitialUrl), userInfo: nil, repeats: true)
    }

    @objc private func reloadWebView() {
        webView.reload()
    }

    @objc private func redirectToInitialUrl() {
        let url = URL(string: "https://Mysurveypage/1234")!
        webView.load(URLRequest(url: url))
    }
}