4

I am able to load a HTML string into the UIWebView with CSS obtained from an online source. The problem I have now is to get the path of the link that was clicked. If I use this:

webView.loadHTMLString(finalHtml, baseURL: baseUrl)

I get the path clicked to be "/" when I println(webView.request?.URL.path) in the UIWebViewDelegate shouldStartLoadWithRequest.

If I use this:

webView.loadHTMLString(finalHtml, baseURL: nil)

I get "nil" when I println(webView.request?.URL.path)

Of course, I have set the baseURL to be the original website, but from my understanding, if the link has the full address, the baseURL is irrelevant.

Any pros out there with any advice on how to get the actual path that is indicated in the tags of the link that was clicked? Thank you in advance =D

EDIT When I long click the link, there will be a popup with the correct link shown. I have tried everything, including absoluteString, but I still won't get the path.

3 Answers 3

1

If webView.loadHTMLString(finalHTML, baseURL: baseURL) and println(webView.request?.URL.path) are called in the same code block, the webView won't have a non-nil request property yet, and so you can't get the URL path on that request. UIWebView is loading your HTML string asynchronously. You can implement UIWebViewDelegate to see when a link is tapped in your UIWebView. Use the following delegate method:

func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
    if navigationType == .LinkClicked {
        println(request.URL.path)
    }
    return true
}

Leave out the if navigationType == .LinkClicked conditional if you want to see the path of all requests including navigation backwards, forwards, etc.

Sign up to request clarification or add additional context in comments.

1 Comment

Of course I already implemented this UIWebViewDelegate to check the path. It just isn't working
0

Have a look at the Documentation for NSURL.

Besides path, there is a property absoluteString that includes the host, base URL etc.

3 Comments

Doesn't work. Just tried. Now I either get the full URL when I use a baseURL, or I get "about:blank" when I use nil
And that is after clicking a link on the website? What content does the page actually load?
Yes. That is after I click a link. It is just a normal html page with links. No javascript at all
0

So it seems that it eluded everyone's eyes that I made a silly mistake in my code. Instead of webView.request?.URL.path, I should use request.URL.path. Really small thing that caused me so much agony. So in case anyone is as silly as me, in your shouldStartLoadWithRequest, use request instead of webView.request.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.