WKWebView: Is it possible to preload multiple URLs?

前端 未结 4 678
梦谈多话
梦谈多话 2021-01-15 03:45

Just migrated an app over to WKWebView and was wondering if there is any possible way to \'preload\' multiple URLs, but only display one at a time?

I ha

4条回答
  •  既然无缘
    2021-01-15 04:40

    Use NSURLCache

    Here is the code

    Swift

    // Create URLRequest
        var request: URLRequest? = nil
        if let url = URL(string: "YOUR_URL") {
            request = URLRequest(url: url)
        }
        
        // Check the cache.
        var cachedResponse: CachedURLResponse? = nil
        if let request = request {
            cachedResponse = URLCache.shared.cachedResponse(for: request)
        }
        print(cachedResponse != nil ? "Cached response found!" : "No cached response found.")
       // Load the cache
        do {
            if let request = request {
                try NSURLConnection.sendSynchronousRequest(request, returning: nil)
            }
        } catch {
        }
    

    Obj-C

    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"YOUR_URL"]];
    
        // Check the cache.
        NSCachedURLResponse *cachedResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:request];
        NSLog(cachedResponse ? @"Cached response found!" : @"No cached response found.");
        //Load cache
        [NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:NULL];
    

提交回复
热议问题