2

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
    No. It isn't. Use activity indicator. – Daniyar Apr 20 '17 at 08:15
  • Please check this question http://stackoverflow.com/questions/35803339/how-to-hold-splashscreen-until-process-in-app-delegate-is-over , maybe you will find something useful for you. – Roman Podymov Apr 20 '17 at 08:19

2 Answers2

3

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.

cdescours
  • 6,004
  • 3
  • 24
  • 30
  • Note: unless supporting iOS 7 and below then OP should be using [WKWebView](https://developer.apple.com/reference/webkit/wkwebview) not `UIWebView`. The accompanying [WKNavigationDelegate](https://developer.apple.com/reference/webkit/wknavigationdelegate) class has a [method](https://developer.apple.com/reference/webkit/wknavigationdelegate/1455629-webview) that is called on completion and [another](https://developer.apple.com/reference/webkit/wknavigationdelegate/1455639-webviewwebcontentprocessdidtermi) that is called on termination – sketchyTech Apr 20 '17 at 08:30
  • Good idea, but as I have multiple WebViews in my App it doesn't work and it would to be to long for the user –  Apr 20 '17 at 08:38
0

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.

Akhil Nair
  • 434
  • 1
  • 6
  • 17