CSWebCache 0.1.2

CSWebCache 0.1.2

Maintained by Mayur Amipara.



  • By
  • Mayur Amipara

CSWebCache

Version License Platform

Requirements

iOS 10.0+ | Xcode 9.0+ | Swift 4.0+

Installation

CSWebCache is available through CocoaPods. To install it, simply add the following line to your Podfile:

use_frameworks!
pod 'CSWebCache'

Usage

You should create an instance of URLCache and set it as the shared cache for your app in your application:didFinishLaunching: method.

    let kB = 1024
    let MB = 1024 * kB
    let GB = 1024 * MB
    let isOfflineHandler: (() -> Bool) = {
        /*
            We are returning true here for demo purposes only.
            You should use Reachability or another method for determining whether the user is
            offline and return the appropriate value
        */
        return true
    }

    let urlCache = CSWebCache.URLCache(memoryCapacity: 20 * MB, diskCapacity: 20 * MB, diskPath: nil,
    CSDiskCapacity: 1 * GB, CSDiskPath: nil, CSSearchPathDirectory: .documentDirectory,
    isOfflineHandler: isOfflineHandler)

    CSWebCache.URLCache.shared = urlCache

To cache a webPage in the CSWebCache disk cache, simply call URLCache's diskCacheURL:loadedHandler: method.

    if let urlToCache = URL(string: "https://appchance.com/blog/how-to-create-your-own-pod") {

        if let cache = URLCache.shared as? CSWebCache.URLCache {

            cache.diskCacheURL(urlToCache, loadedHandler: { (webView) -> (Bool) in
                let state = webView.stringByEvaluatingJavaScript(from: "document.readyState")
                if state == "complete" {
                    // Loading is done once we've returned true
                    return true
                }
                return false
            }, completeHandler: { () -> Void in
                print("Finished caching")
            }, failureHandler: { (error) -> Void in
                print("Error caching: \(error)")
            })
        }

        webview.loadRequest(URLRequest(url: urlToCache))
    }

Author

Mayur Amipara

License

CSWebCache is available under the MIT license. See the LICENSE file for more info.