Latest 0.1.2
Homepage https://github.com/Mayuramipara94/CSWebCache
License MIT
Platforms ios 10.0, requires ARC
Authors

Version
License
Platform

Requirements

iOS 10.0+ | Xcode 10.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.

Latest podspec

{
    "name": "CSWebCache",
    "version": "0.1.2",
    "summary": "iOS Offline Caching for Webview Content",
    "swift_version": "4.0",
    "platforms": {
        "ios": "10.0"
    },
    "description": "TODO: A Swift framework for storing entire web pages into a disk cache distinct from, but interoperable with, the standard URLCache layer. This is useful for both pre-caching web content for faster loading, as well as making web content available for offline browsing.",
    "homepage": "https://github.com/Mayuramipara94/CSWebCache",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Mayur Amipara": "[email protected]"
    },
    "source": {
        "git": "https://github.com/Mayuramipara94/CSWebCache.git",
        "tag": "0.1.2"
    },
    "pod_target_xcconfig": {
        "SWIFT_INCLUDE_PATHS[sdk=iphoneos*]": "$(PODS_ROOT)/CommonCrypto/CocoaPods/iphoneos",
        "SWIFT_INCLUDE_PATHS[sdk=iphonesimulator*]": "$(PODS_ROOT)/CommonCrypto/CocoaPods/iphonesimulator"
    },
    "requires_arc": true,
    "source_files": "CSWebCache/Classes/**/*",
    "xcconfig": {
        "HEADER_SEARCH_PATHS": "$(SDKROOT)/usr/include/CommonCrypto/CommonCrypto.h"
    }
}

Pin It on Pinterest

Share This