Lily.of.the.Valley Lily.of.the.Valley - 2 months ago 9
iOS Question

UIWebView webViewDidStartLoad Method not getting called in swift

I have implemented a simple webview in ios with a PDF being displayed, and I want to do 2 things:
1- remove the gray padding area around the pdf doc when it's shown on webview
2-display a button in a specific location over the pdf file given the coordinates of the button

For both the two issues above, I have found code on SO and changed them to my needs. Here is the code I have so far (the view controller)

class DocAreaController: UIViewController, UIWebViewDelegate{


@IBOutlet var myWebView: UIWebView!


override func viewDidLoad() {
super.viewDidLoad()

myWebView.scrollView.scrollEnabled = false
myWebView.scrollView.bounces = false
myWebView.scrollView.pagingEnabled = true
myWebView.userInteractionEnabled = true
myWebView.delegate = self

func webViewDidStartLoad(myWebView: UIWebView){
let paddingScript = "document.body.style.margin='0';document.body.style.padding = '0'"
let result = myWebView.stringByEvaluatingJavaScriptFromString(paddingScript)
debugPrint("method called")

let btn: UIButton = UIButton(frame: CGRectMake(188, 340, 46, 30))
btn.backgroundColor = UIColor.cyanColor() //.colorWithAlphaComponent(0.5)
btn.userInteractionEnabled = false
btn.tag = 1 // change tag property
myWebView.addSubview(btn) // add to view as subview

}


// Get the document's file path.
let filepath = (NSBundle.mainBundle().pathForResource("Reader", ofType: "pdf"))! as String

// Create an NSURL object based on the file path.
let url = NSURL.fileURLWithPath(filepath)

// Create an NSURLRequest object.
let request = NSURLRequest(URL: url)

// Load the web viewer using the request object.
myWebView.loadRequest(request)

}


}

But the webViewDidStartLoad method is not getting called. I have set the delegate for webview both in code and storyboard. What m I missing?

Answer

Shift the webViewDidStartLoad out of viewDidLoad method. Place it after the viewDidLoad and it should work.

override func viewDidLoad() {
    super.viewDidLoad()

    myWebView.scrollView.scrollEnabled = false
    myWebView.scrollView.bounces = false
    myWebView.scrollView.pagingEnabled = true
    myWebView.userInteractionEnabled = true
    myWebView.delegate = self

    // Get the document's file path.
    let filepath = (NSBundle.mainBundle().pathForResource("Reader", ofType: "pdf"))! as String

    // Create an NSURL object based on the file path.
    let url = NSURL.fileURLWithPath(filepath)

    // Create an NSURLRequest object.
    let request = NSURLRequest(URL: url)

    // Load the web viewer using the request object.
    myWebView.loadRequest(request)

}


func webViewDidStartLoad(myWebView: UIWebView){
        let paddingScript = "document.body.style.margin='0';document.body.style.padding = '0'"
        let result = myWebView.stringByEvaluatingJavaScriptFromString(paddingScript)
        debugPrint("method called")

        let btn: UIButton = UIButton(frame: CGRectMake(188, 340, 46, 30))
        btn.backgroundColor = UIColor.cyanColor() //.colorWithAlphaComponent(0.5)
        btn.userInteractionEnabled = false
        btn.tag = 1               // change tag property
        myWebView.addSubview(btn) // add to view as subview

    }
Comments