Xeven Elewen Xeven Elewen - 2 months ago 24
HTML Question

UIWebView white flashing when load page

i have build a WebApp for iOS. When i click/tap to move to a page for example from page - index.html to page2.html it flashing white. Did you know why, and how to solve this problem?

The Websites works fantastic in Browser (Safari, IE, Firefox and Chrome) but not as an Application.

My code is:

import UIKit

class ViewController: UIViewController {


@IBOutlet weak var webView: UIWebView!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.


webView.loadRequest(NSURLRequest(URL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("index", ofType: "html")!)))


}

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


}

Answer

Try my code will help you!

Step 1: Fetch UIWebView and UIActivityIndicatorView to your view controller. Step 2: Delegate UIWebView to viewController and Create outlets for UIWebView and UIActivityIndicatorView.

Step 3: Select UIActivityIndicatorView on view controller then go to Attributes Inspector ->Activity Indicator View -> check Animating and Hides when Stopped on Behaviour

Step 4: Add Below codes to your ViewController.

import UIKit

class ViewController: UIViewController,UIWebViewDelegate{

    @IBOutlet var loader: UIActivityIndicatorView!
    @IBOutlet var webView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.automaticallyAdjustsScrollViewInsets = false //to avoid auto scrolling.
        functionOfWebView()

    }

    func functionOfWebView()
    {
        let URL = NSURL(string: "http://www.google.com")
        //let URL = NSBundle.mainBundle().URLForResource("index", withExtension: "html")  //For local html file(index.html) with local file hyperlink(file.html) see on video tutorial
        let request = NSURLRequest(URL: URL!)
        webView.loadRequest(request)
    }

    func webViewDidStartLoad(webView: UIWebView)
    {
        loader.startAnimating()
    }

    func webViewDidFinishLoad(webView: UIWebView)
    {
        loader.stopAnimating()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }


}

In Swift 3.0

import UIKit

class ViewController: UIViewController,UIWebViewDelegate{

    @IBOutlet var loader: UIActivityIndicatorView!
    @IBOutlet var webView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.automaticallyAdjustsScrollViewInsets = false //to avoid auto scrolling.
        functionOfWebView()
    }

    func functionOfWebView()
    {
        let URL = NSURL(string: "http://www.google.com")
        //let URL = Bundle.main.url(forResource: "index", withExtension: "html")  //For local html file(index.html) with local file hyperlink(file.html) see on video tutorial
        let request = NSURLRequest(url: URL! as URL)
        webView.loadRequest(request as URLRequest)
    }

    func webViewDidStartLoad(_ webView: UIWebView)
    {
        loader.startAnimating()
    }

    func webViewDidFinishLoad(_ webView: UIWebView)
    {
        loader.stopAnimating()
    }

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

}

enter image description here

if you confuse or for Using Local html files then see this Youtube tutorial