How to know the current URL address of the webView?

I have a webView in my app, and I want to know the current URL address that is shown in the webView.

The user might have clicked some links in the webpage or the Forward/Backward buttons (implemented in my webView), so the current URL address is changed by the user’s operations.

I originally thought I could get this URL address by the listener, such as

local function webListener( event )     if (event.url ~= nil and (event.type == "link" or event.type == "loaded") then         updateCurrLinkAddress(event.url)     end end

This way works for most cases but if the user clicks Forward/Backward buttons, this method does not work.

I can’t just use any event.url as the link address because the webpage might have other webpage embedded, such as Facebook plugins or some ad pages. The listener reports all kind of links.

Is it possible for Corona SDK to set the current URL address into a property? and I can just read this property. Is it there already?

This is important when you want to share the link inside the webView. This feature is required by so many users of my app. They browse the net via my webView and suddenly they like the page and they want to share it. Although they can share via webpage share links, but you know the users, they don’t care, they just think your app sucks because you don’t provide such a feature. And besides, share things via app is more intuitive & straightfoward.

Hi Joe,

Did you ever figure this out? I need to do the same thing. I have a list of items in the webView and when one is clicked it adds url parameter and I need to read it. I guess since the user is not using the forward/backward buttons I might can try your method above which I will. But if you have a better way I would love to hear it!T

Thanks,

Warren

Although Corona claims “loaded” event problem is fixed for actual iOS device, I found there are cases the loaded event is still missing.

Therefore, I cannot use loaded event to determine the current link address. Besides, what if there is some Internet problem and there is no page loaded? (no loaded event, however I still want to know what the current link address is)

I then use following “strange” code to determine the current link address:

if (event.type ~= nil and event.url ~= nil and event.url ~= "about:blank") then if (event.type == "history") then if (backwardForwardClicked == true) then updateCurrLinkAddress(event.url) backwardForwardClicked = false end elseif (event.type ~= "other" and event.type ~= "form" and event.type ~= "reload") then updateCurrLinkAddress(event.url) backwardForwardClicked = false end end

backwardForwardClicked is set to true when backward/forward button is clicked.

This “strange” formula is based on the event patterns generated by the webView through different operations (backward/forward buttons, link clicked, etc.) It can only achieve 90% accuracy (better than using the loaded event to determine) since I know there is still some case that this formula would fail. This is the best I can achieve so I have to live with it even I know it has flaws.

Corona should implement a property in webView to tell what the current link is. It’s the best & only solution for this.

webView has many other problems & has been the weakest link in my app. I really hope Corona can identify this reality & enhance it in every aspect (possibly a rewrite for webView module).

Hi Joe,

Did you ever figure this out? I need to do the same thing. I have a list of items in the webView and when one is clicked it adds url parameter and I need to read it. I guess since the user is not using the forward/backward buttons I might can try your method above which I will. But if you have a better way I would love to hear it!T

Thanks,

Warren

Although Corona claims “loaded” event problem is fixed for actual iOS device, I found there are cases the loaded event is still missing.

Therefore, I cannot use loaded event to determine the current link address. Besides, what if there is some Internet problem and there is no page loaded? (no loaded event, however I still want to know what the current link address is)

I then use following “strange” code to determine the current link address:

if (event.type ~= nil and event.url ~= nil and event.url ~= "about:blank") then if (event.type == "history") then if (backwardForwardClicked == true) then updateCurrLinkAddress(event.url) backwardForwardClicked = false end elseif (event.type ~= "other" and event.type ~= "form" and event.type ~= "reload") then updateCurrLinkAddress(event.url) backwardForwardClicked = false end end

backwardForwardClicked is set to true when backward/forward button is clicked.

This “strange” formula is based on the event patterns generated by the webView through different operations (backward/forward buttons, link clicked, etc.) It can only achieve 90% accuracy (better than using the loaded event to determine) since I know there is still some case that this formula would fail. This is the best I can achieve so I have to live with it even I know it has flaws.

Corona should implement a property in webView to tell what the current link is. It’s the best & only solution for this.

webView has many other problems & has been the weakest link in my app. I really hope Corona can identify this reality & enhance it in every aspect (possibly a rewrite for webView module).

+1 for a property in webView to tell what the current url is!

I’m currently using some hackish urlRequest event properties filtering technics but it’s impossible to get current url while browsing Pinterest as they swap it dynamically and load data via AJAX.

+1 for a property in webView to tell what the current url is!

I’m currently using some hackish urlRequest event properties filtering technics but it’s impossible to get current url while browsing Pinterest as they swap it dynamically and load data via AJAX.