/FirebaseStorageCache

FirebaseStorage for iOS with caching and offline capabilities

Primary LanguageSwiftMIT LicenseMIT

FirebaseStorageCache

FIRStorage for iOS with caching and offline capabilities

Version license Platform license

Demo

Clone/download FirebaseOfflineAppDemo and run pod install before pressing play in Xcode. The demo contains 3 examples: no caching, NSCache and FirebaseStorageCache.

Requirements

This project assumes that you have already setup Firebase for iOS.

Installation

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

pod 'FirebaseStorageCache'

Usage

Use the default shared cache

let ref: StorageReference = ...
FirebaseStorageCache.main.get(storageReference: ref) { data in
  // do something with your file
}

Create custom storage caches

let oneWeekDiskCache = DiskCache(name: "customCache", cacheDuration: 60 * 60 * 24 * 7)
let firStorageCache = FirebaseStorageCache(cache: oneWeekDiskCache)
firStorageCache.get(storageReference: ref) { data in
  // do something with your file
}

Extension for loading images (in UIImageView)

imageView.setImage(storageReference: ref)

Extension for loading web pages (in UIWebView and WKWebView)

Simple:

webView.loadHTML(storageReference: ref)

With post processing on the HTML:

let styleHTML: (Data) -> Data = { data in
            let pre = "<style>body {margin: 16px}</style>"
            var preData = pre.data(using: .utf8) ?? Data()
            preData.append(data)
            return preData
        }
webView.loadHTML(storageReference: ref, postProcess: styleHTML)

Cleaning/pruning the cache

In the didFinishLaunchingWithOptions of your AppDelegate, you should call the prune() method of your disk caches to remove any old files:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        FirebaseApp.configure()
        FirebaseStorageCache.main.prune()
        return true
    }

Author

Antony Harfield, antonyharfield@gmail.com

License

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