Swift 3, iOS 10.3 - Preload UIWebView during Launch Screen

柔情痞子 提交于 2019-12-24 05:58:34

问题


I have an App, which has multiple WebViews on different ViewControllers but it takes about 3 seconds to load each of them. Is it possible to start loading the webpages during the Launch Screen or to load the WebView of the SecondViewController when the user is on the webpage on the FirstViewController?

    import UIKit

    class dabs: UIViewController {
        @IBOutlet weak var webView_dabs: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        let websiteURL = URL(string: "https://www.skybriefing.com/portal/dabs")
        let websiteURLRequest = URLRequest(url: websiteURL!)
        webView_dabs.loadRequest(websiteURLRequest)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

回答1:


Maybe the best way to do that is to put above your view controller's view a temporary view that is exactly the same as your splash screen.

Add your view controller as a delegate to your UIWebView and in the method webViewDidFinishLoad: just dismiss the temporary view.

In theory, the user will just believe that the splashscreen has taken a bit more time.




回答2:


Make the use of activity indicator in your web view, such that when the website is loading the activity indicator will start animating and when the website loads completely, the animation is stopped.

Add a web view and activity indicator in your storyboard.

Now add this code in your swift file.

import UIKit

class WebViewController: UIViewController, UIWebViewDelegate {

@IBOutlet var webView: UIWebView!

@IBOutlet var activityIndicator: UIActivityIndicatorView!

override func viewDidLoad() {

    super.viewDidLoad()

    // Do any additional setup after loading the view.

    let url = URL(string: "https://google.com")

    let request = URLRequest(url: url!)

    webView.loadRequest(request)

    webView.scalesPageToFit = true
}

func webViewDidStartLoad(_ webView: UIWebView) {

     print("Activity indicator start")

    activityIndicator.startAnimating()

}

func webViewDidFinishLoad(_ webView: UIWebView) {

     print("Activity indicator stop")

    activityIndicator.stopAnimating()

}
}

Make sure to add delegate in your class.



来源:https://stackoverflow.com/questions/43513846/swift-3-ios-10-3-preload-uiwebview-during-launch-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!