Closed TheMen4ce closed 3 years ago
Hi TheMen4ce,
this project is only an example app for the https://github.com/ernesto-elsaesser/WebArchiver package, to demonstrate its usage. If you develop your own app, all you should need is the WebArchiver.
Further, the WebArchiver is actually a one method package. It provides the WebArchiver.archive method,
which takes any URL and creates a WebArchive from it. This archive can be saved on disk and later loaded into a WebView
. That's all. See the archive
and unarchive
methods in https://github.com/ernesto-elsaesser/OfflineWebView/blob/master/OfflineWebView/ContentView.swift.
Everything else - especially when and how you choose to create and load such archives - is completely up to your use case. For the described scenario, if you only have a fixed list of articles, you could create archives up front. If articles are being added continuously, you have to decide at which point the app should create archives.
I myself use the package to create archives of every page that the user navigates to, in order to have them available offline later.
The decision whether to load an archive or the online version of a page into the WebView
is also up to you. In my app, the archive is only loaded when the app has no internet connection. But you could also decide to load an archive whenever the
user navigates to a page for which an archive is available. Or you could even try to serve all pages from archives. In this case, of course, you'd have to make sure that archives for all pages are available up front.
Note that the navigation between pages doesn't happen on archive level, it happens on URL level. The WebArchiver doesn't change the links inside your HTML, so if there is a link on your example.com/blog page that links to example.com/blog/article-a, the same link will be present in the version of the page loaded from an archive. So when your user clicks on that link, the WebView will try to navigate to example.com/blog/article-a. At this point it is up to you to decide how to serve that URL - from the web of from an archive (see https://developer.apple.com/documentation/webkit/wknavigationdelegate).
Finally, if you can make your app work with built-in caching, then there is no need to use the WebArchiver at all. The only reason I wrote the WebArchiver was because I couldn't get WebKit to cache all the resources of visited page, i.e. all images, style sheets, JavaScript, etc - at least not without tweaking the HTTP caching headers of those pages, which were beyond my control. But if this is no issue for you, for example because you control the server side as well, relying on the built-in caching should be by far the easier option.
Hope this helps, Ernesto
Hi Ernesto,
Wow, what an answer!
That's exactly what I've implemented and it works pretty smooth.
Thank you very much and best regards, Dennis
Hi there, this is more of a question than an issue...
I have two layer or levels of pages that I want to have available offline:
Now when I'm on /blog, I can never reach /blog/article-a even though /blog/article-a was archived before. How can I cache/archive both layers of my site and have the links between them still work?
To have only the first page in cache the OfflineWebView nor the WebArchiver would be need. I can get the same behaviour just using
NSURLRequest.CachePolicy.returnCacheDataElseLoad
for theURLRequest
in theWKWebView
. This already gives me an offline functionality for the first layer. Correct me if I'm wrong here.Thanks a lot :)