Alessandro Zago Alessandro Zago - 3 months ago 17
Swift Question

Swift onfinish load event of a UIWebView

I'm loading a pdf in

WebView
from my server with this code :

webView = UIWebView(frame: CGRectMake(0, y, screenSize.width, screenSize.height-y))
let url : NSURL! = NSURL(string: urlFile)
webView?.loadRequest(NSURLRequest(URL: url))
webView?.opaque = false;
webView?.backgroundColor = UIColor.clearColor()
webView?.scalesPageToFit = true;
self.view.addSubview(webView!)


This code works but how can i receive an event "onPageLoad"?
sorry for bad english, i'm italian(:

Answer

You need to implement UIWebViewDelegate like this and use webViewDidFinishLoad to know the page is loaded successfully, for that set the webView delegate with your viewController like this, and implement the webViewDidFinishLoad method inside your viewController like below example.

import UIKit
class ViewController: UIViewController, UIWebViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        webView = UIWebView(frame: CGRectMake(0, y, screenSize.width, screenSize.height-y))
        let url : NSURL! = NSURL(string: urlFile)
        webView?.loadRequest(NSURLRequest(URL: url))
        webView?.opaque = false;
        webView?.backgroundColor = UIColor.clearColor()
        webView?.scalesPageToFit = true; 
        webView?.delegate = self// Add this line to set the delegate of webView
        self.view.addSubview(webView!)       
    }

    func webViewDidFinishLoad(webView : UIWebView) {
        //Page is loaded do what you want
    }
}