Search code examples
iosuiwebviewpage-refreshetag

How to tell when UIWebView content changes?


I am working on an iOS App using Xcode. The app refreshes a UIWebView every X number of seconds.

By way of background, the app is called 'Browser Auto Refresh': https://itunes.apple.com/us/app/browser-auto-refresh/id1177883066?mt=8).

I am trying to implement an alert when the content of the UIWebView changes post a page refresh (i.e. the page currently being refreshed has just been updated by the owner of that page). I have managed to get hold of the response headers but cannot see any eTag info from multiple sites (incl Google.com).

Does anyone know how you can tell that the content of a webpage has changed when refreshing a UIWebView page?


Solution

  • As Peter Pajchl stated, the contents that your UIWebView gets on its plate are not generally under your control. This means that - unless you only rely on a fixed set of services strictly under your control - you cannot rely on HTTP headers to find out any helpful meta-information.

    As he also suggested, you can calculate a checksum of the page contents (that's what a strong Etag would most likely do anyways) and compare it to the newly fetched version. The downside is that the method is inherently strong, and you will get false positives if you are only interested in semantical updates. To render your application useless for a specific site, the owner could just inject a timestamp somewhere into the contents, since every update would yield a different checksum.

    You could hence try to normalize the data before calculating a checksum in a way that only takes meaningful data into account. This most likely requires a priori knowledge of the site you are watching in order to be effective.

    Another way would be to calculate some kind of similarity metric (with or without normalization) and have a threshold on the "updated" alert. This can be computationally demanding, and what's worse, it may require even more a priori information (at least the metaparameters like the treshold).