6

Cache manifest works fine and events fired in safari in IOS 8. Not working at all in WKWebView anyone else solve this issue?

import UIKit

import WebKit

class ViewController: UIViewController {
@IBOutlet var containterView : UIView! = nil
var webView : WKWebView?
override func loadView(){
    super.loadView()
    self.webView = WKWebView()
    self.view = self.webView!
}
override func viewDidLoad() {
    super.viewDidLoad()
    var url = NSURL(string:"http://html5demos.com/offlineapp")
    var req = NSURLRequest(URL:url)
    self.webView!.loadRequest(req)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

}

The application cache comes back as supported if I were to use html5test.com

EDIT:

window.applicationCache does not return undefined either when loaded from WKWebView

console.log("Initializing Page");       
if (window.applicationCache == undefined){
    console.log("Application cache not suported!");
    updateSplash();
}
console.log(window.applicationCache); returns: DOMApplicationCache

EDIT 2:

if (typeof window.applicationCache.update === 'function'){
        console.log("Application has method update");
        console.log(window.applicationCache.update); //shows swapCache() and update() methods
        window.applicationCache.update();   
    }

window.applicationCAche.update() throws Error: InvalidStateError: DOM Exception 11: An attempt was made to use an object that is not, or is no longer, usable.

KleggerKoder
  • 161
  • 1
  • 10
  • 1
    I don't see why someone would downvote this. ApplicationCache is a major feature and not being able to use it with WKWebView is a big throwback. – Francesco Frapporti Nov 05 '14 at 01:35
  • This caused me major headaches. Why would they support localstorage and not a cache manifest? Any Ideas on how to get around this using native functionality instead? Looks like I will have to store all of the html/css/js/images all on the device...why apple why! – Fostah Sep 01 '15 at 21:34

2 Answers2

2

Just for the record, this question appears to have been asked on and linked from the Apple Developer Forums. The official response from Apple is that the HTML5 Application Cache functionality is not available in WKWebView:

The offline application cache is not enabled in WKWebView. Feel free to request that it be made available via https://bugreport.apple.com.

Michael McGuire
  • 3,770
  • 2
  • 29
  • 28
  • 1
    We can solve this problem using private apis ...one set back to this approach is ...we can't use appstore version apps...if its enterprise apps we can use it..here is the solution http://goo.gl/jnyX8T – Durai Amuthan.H Feb 01 '16 at 08:45
  • I couldn't get to that forum post, but found a new [post](https://forums.developer.apple.com/message/160199#160199) from August 2016 basically saying the same thing. – Amelia Oct 31 '16 at 17:36
1

I think you are trying to solve the same problem as I do. This is what I do.

  1. Save the start page of your web app into one HTML file(index.html), embedding everything (CSS, JS, images as base 64, icon fonts). And add that file into your Xcode project.
  2. Start the app by reading the content of the HTML file and load it in your WKWebView. You can set the base as the same url you are supposed to start with. This way, it'll be as if the web app is opened on your web site.

The benefit is that your app will always start even when the user's network isn't good. Here's the SWIFT code that I use, courtesy of Matt Neuberg (https://books.google.com/books?id=wLaVBQAAQBAJ&pg=PT669&lpg=PT669&dq=addConstraints+wkwebview&source=bl&ots=7trE7MR1zR&sig=VT6GDBGbDw9dh89wDb5Uajd4gUY&hl=en&sa=X&ei=cyyeVNH4MM3ToATukoDgAQ&ved=0CDkQ6AEwBA#v=onepage&q=addConstraints%20wkwebview&f=false). If you want the full source code, please let me know and I'll post it on Github.

    let templatepath = NSBundle.mainBundle().pathForResource("index", ofType: "html")!
    let base = NSURL(string:"http://m.ftchinese.com/iphone-2014.html#iOSShare")
    var s = NSString(contentsOfFile:templatepath, encoding:NSUTF8StringEncoding, error:nil)!
    self.webView!.loadHTMLString(s, baseURL:base)
Oliver Zhang
  • 499
  • 6
  • 16
  • This has nothing to do with the question asked. They are looking to use the HTML 5 Application Cache, which does not appear to be supported in WKWebView (https://devforums.apple.com/message/1060452) – Michael McGuire Jan 07 '15 at 15:56