javascript - How can I "catch" the back and forward requests made to UIWebView programmatically? -
javascript calls these:
history.goback(); history.goforward(); history.go(-1);
make uiwebview go backwards or forwards programmatically. need way know user going before happens. may solution listening delegate there's no such thing legitimate responses when comes uiwebview.
this delegate method called when user clicks "back" within page:
- (bool)webview:(uiwebview *)webview shouldstartloadwithrequest:(nsurlrequest *)request navigationtype:(uiwebviewnavigationtype)navigationtype
it's navigationtype set linkclicked , not backforward expected. have seen plenty craziness uiwebview surprised little bit, it's dead-end type of problem me.
i have 2 propositions:
create custom javascript method "mywayofgoingback();" , override goback(); methods when it's clicked method informs me of going happen. add listener methods above don't know how javascript works.any ideas?
i'm little confused you're asking. you've got uiwebview you're using show user website, , website has clickable , forwards buttons/links built in? right? website control, or sending them 3rd party site?
and regardless, know page webview going load before loads. right?
the problem (bool)webview:(uiwebview *)webview shouldstartloadwithrequest:(nsurlrequest *)request navigationtype:(uiwebviewnavigationtype)
has no back/forward navigation type built in. if wanted, maintain global array of every page visit (update within webview: function), , compare new request list see if they're visiting pages again. or can inject js override link functionality within javascript
nsstring* js = @"function reportbacktoobjectivec(string){var iframe = document.createelement('iframe');iframe.setattribute('src', 'callback://' + string);document.documentelement.appendchild(iframe);iframe.parentnode.removechild(iframe);iframe = null;}var links = document.getelementsbytagname('a');for (var i=0; i<links.length; i++) {links[i].addeventlistener('click', function() {reportbacktoobjectivec('something_happened');}, true);}"; [webview stringbyevaluatingjavascriptfromstring:js];
(sorry doesn't fit on screen, it's compressed create injectable. can re-create , paste out , expand bit see what's going on).
that add together event listener every link fires request url callback://something_happened whenever user clicks on link. can filter sorts of requests within delegate, , deed on them accordingly.
does help? or missing question?
javascript ios objective-c uiwebview
No comments:
Post a Comment