3
votes

I'm developing a Backbone.js based web app that for some use cases will be wrapped inside a native iOS UIWebView. In the iOS app – when we want to change page in the web view – we simply initiate a new request on the web view, which loads the page accordingly.

My only problem is that when there is a Backbone route defined for the requested URL, I want the web view to use Backbone's routing and pushState instead of initiating a whole new page load. However, when there isn't any Backbone route corresponding to the requested URL, the web view should handle it as a whole new page request and "reload" the page with that URL.

Right now the native app uses UIWebView's loadRequest: method and passes a new NSURLRequest, with a URL of e.g. "http://example.com/search", as parameter.

Right now my only idea is to use UIWebView's stringByEvaluatingJavaScriptFromString: method:

  • Have the web view call some JS function that in turn will check if my Backbone routes can handle that URL.
  • If yes, it should trigger the router callback.
  • If not, it should initiate a new page request with the URL.

Any input? Is there any built in way of solving this?

Thanks!

1

1 Answers

1
votes

Could you just turn of the pushState use for the iOS app? I assume the url is not visible to the user in the app anyway, so the downside of uglier urls does not apply. So instead of changing the url to /items/3 you would change it to #/items/3.

This would mean changes that need to be handled by backbone would just change the hash and changes that need a new page would change the location.