I have a WebKit-based application. When the user initiates an action that opens a new page (such as clicking on a link), I would like to intercept that URL and choose to to open it in my application, or kick it out to Safari.
Ideally, I’d like to identify the first request in a loading sequence, or the one that directly resulted from a user action. Alternatively, it might be enough if I could identify changes to the top-level URL in a frame, with the caveat that I need to be able to cancel the request if I decide to bounce it to Safari instead.
I have implemented webView:decidePolicyForNavigationAction:request:frame:decisionListener, and I can see all of the requests made during the loading of the page. Both the requests triggered by the user, as well as those kicked off by Javascript, REST calls, that kind of thing.
For simple links, I can get the WebActionNavigationType from the action information. But if the user clicks on an element with a Javascript onclick() handler, which in turn navigates to a different page, the navigation type will be “Other”, and indistinguishable from all other Javascript requests (i.e. secondary REST calls resulting from the original request).
Any one else tackled this? Any pointers? Thanks!
I figured out something that seems to work for me. In decidePolicyForNavigationAction, this test will determine if it was a user-initiated action: