FIRStorage for iOS with caching and offline capabilities
Clone/download FirebaseOfflineAppDemo and run pod install
before pressing play in Xcode. The demo contains 3 examples: no caching, NSCache and FirebaseStorageCache.
This project assumes that you have already setup Firebase for iOS.
FirebaseStorageCache is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'FirebaseStorageCache'
let ref: StorageReference = ...
FirebaseStorageCache.main.get(storageReference: ref) { data in
// do something with your file
}
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
}
imageView.setImage(storageReference: ref)
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)
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
}
Antony Harfield, antonyharfield@gmail.com
FirebaseStorageCache is available under the MIT license. See the LICENSE file for more info.