使用swift在WKWebView上显示活动指示器

问题描述:

我正在处理以下代码并尝试在页面加载时在视图中显示活动指示器。

I am working on the following code and trying to show an activity indicator in the view whilst the page is loading..

我试图实现 WKNavigationDelegate 方法,但我失败了,因为没有显示。

I tried to implement the WKNavigationDelegate methods but I am failing as nothing shows.

有关如何解决此问题的任何建议吗?

Any suggestions on how to fix this?

我没有设置SupportWebView视图委托在任何地方,但我不知道如何在swift中进行..

I am not setting the SupportWebView view delegate anywhere but I wouldn't know how to do it in swift..

import UIKit
import WebKit

class SupportWebView: UIViewController, WKNavigationDelegate {
    @IBOutlet var containerView : UIView? = nil

    var webView: WKWebView?

    override func loadView() {
        super.loadView()
        self.webView = WKWebView()
        self.view = self.webView
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        var dataManager = DataManager.sharedDataManager()
        var url = dataManager.myValidURL
        var req = NSURLRequest(URL:url!)
        self.webView!.loadRequest(req)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func webView(webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        UIApplication.sharedApplication().networkActivityIndicatorVisible = true
    }


    func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
        UIApplication.sharedApplication().networkActivityIndicatorVisible = false
    }
}


如评论所述,您忘记设置 webView 委托:

As commented, you forgot to set the webView delegate:

override func loadView() {
    super.loadView()
    self.webView = WKWebView()
    self.webView.navigationDelegate = self
    self.view = self.webView
}