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'letref:FIRStorageReference=...FirebaseStorageCache.main.get(storageReference: ref){ data in
// do something with your file
}letoneWeekDiskCache=DiskCache(name:"customCache", cacheDuration:60*60*24*7)letfirStorageCache=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:
letstyleHTML:(Data)->Data={ data inletpre="<style>body {margin: 16px}</style>"varpreData= 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{FIRApp.configure()FirebaseStorageCache.main.prune()returntrue}Antony Harfield, antonyharfield@gmail.com
FirebaseStorageCache is available under the MIT license. See the LICENSE file for more info.