user7890983
user7890983

Reputation:

Swift 3, iOS 10.3 - Preload UIWebView during Launch Screen

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()
    }
}

Upvotes: 2

Views: 1953

Answers (2)

Akhil Nair
Akhil Nair

Reputation: 444

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.

Upvotes: 0

cdescours
cdescours

Reputation: 6022

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.

Upvotes: 3

Related Questions