{"id":827,"url":"https://github.com/buzzfeed/mattress","last_synced_at":"2025-10-09T19:43:30.369Z","repository":{"id":23199416,"uuid":"26556024","full_name":"buzzfeed/mattress","owner":"buzzfeed","description":"iOS Offline Caching for Web Content","archived":false,"fork":false,"pushed_at":"2017-08-19T16:09:41.000Z","size":1195,"stargazers_count":522,"open_issues_count":4,"forks_count":45,"subscribers_count":234,"default_branch":"master","last_synced_at":"2025-03-29T11:07:00.726Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/buzzfeed.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-11-12T21:02:31.000Z","updated_at":"2025-02-14T10:17:39.000Z","dependencies_parsed_at":"2022-08-21T21:31:32.093Z","dependency_job_id":null,"html_url":"https://github.com/buzzfeed/mattress","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/buzzfeed%2Fmattress","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/buzzfeed%2Fmattress/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/buzzfeed%2Fmattress/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/buzzfeed%2Fmattress/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/buzzfeed","download_url":"https://codeload.github.com/buzzfeed/mattress/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247332602,"owners_count":20921853,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-01-05T20:15:32.279Z","updated_at":"2025-10-09T19:43:30.295Z","avatar_url":"https://github.com/buzzfeed.png","language":"Swift","readme":"Mattress\n========\nA Swift framework for storing entire web pages into a disk cache distinct from, but interoperable with, the standard NSURLCache layer. This is useful for both pre-caching web content for faster loading, as well as making web content available for offline browsing.\n\n**Requirements**\n----------------\n\n- iOS 7.0+ (iOS 8 required for integration as an embedded framework)\n\n**Installation**\n----------------\n\nMattress includes a wrapper around CommonCrypto so that it can be easily used from within Swift. You will need to make sure you include both the Mattress and CommonCrypto frameworks in your project.\n\n**Carthage (Recommended)**\n\nIf you are not already using Carthage, you will need to install it using [Homebrew](http://brew.sh).\n\n```\n$ brew update\n$ brew install carthage\n```\n\nOnce installed, add it to your Cartfile:\n\n```\ngithub \"buzzfeed/Mattress\" \u003e= 1.0.0\n```\n\nYou will then need to build using Carthage, and manually integrate both the Mattress and CommonCrypto frameworks into your project.\n\n```\n$ carthage build\n```\n\n**CocoaPods**\n\nIf you are not already using CocoaPods, you will need to install it using RubyGems.\n\n```\n$ gem install cocoapods\n```\n\nOnce installed, add it to your Podfile:\n\n```ruby\npod 'Mattress', '~\u003e 1.0.0'\n```\n\n**Manual**\n\n1. Open the `Mattress` folder, and drag `Mattress.xcodeproj` into the file navigator of your app project. **NOTE: The Mattress project needs to be added somewhere under the target project or you won't be able to add it to your target dependencies.**\n2. Ensure that the deployment target of the Mattress project matches that of the application target.\n3. In your target's \"Build Phases\" panel, add `Mattress.framework` to the \"Target Dependencies\"\n4. Click on the `+` button at the top left of the panel and select \"New Copy Files Phase\". Rename this new phase to \"Copy Frameworks\", set the \"Destination\" to \"Frameworks\", and add both `Mattress.framework` and `CommonCrypto.framework`.\n\n**Usage**\n---------\nYou should create an instance of URLCache and set it as the shared\ncache for your app in your application:didFinishLaunching: method.\n\n```swift\nfunc application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -\u003e Bool {\n    let kB = 1024\n    let MB = 1024 * kB\n    let GB = 1024 * MB\n    let isOfflineHandler: (() -\u003e Bool) = {\n        // This is for demonstration only.\n        // You should use Reachability or your preferred method to determine offline status\n        // and return a proper value here.\n        return false\n    }\n    let urlCache = Mattress.URLCache(memoryCapacity: 20 * MB, diskCapacity: 20 * MB, diskPath: nil,\n    \tmattressDiskCapacity: 1 * GB, mattressDiskPath: nil, mattressSearchPathDirectory: .DocumentDirectory,\n    \tisOfflineHandler: isOfflineHandler)\n    \n    NSURLCache.setSharedURLCache(urlCache)\n    return true\n}\n```\n\nTo cache a webPage in the Mattress disk cache, simply call URLCache's diskCacheURL:loadedHandler: method.\n\n```swift\nNSLog(\"Caching page\")\nlet urlToCache = NSURL(string: \"https://www.google.com\")\nif let\n    cache = NSURLCache.sharedURLCache() as? Mattress.URLCache,\n    urlToCache = urlToCache\n{\n    cache.diskCacheURL(urlToCache, loadedHandler: { (webView) -\u003e (Bool) in\n            /*\n               Note: The below code should work for fairly simple pages. However, if the page you are\n               attempting to cache contains progressively / lazily loaded images or other assets you\n               will also need to trigger any JavaScript functions here to mimic user actions and\n               ensure those assets are also loaded before returning true.\n            */\n            let state = webView.stringByEvaluatingJavaScriptFromString(\"document.readyState\")\n            if state == \"complete\" {\n                // Loading is done once we've returned true\n                return true\n            }\n            return false\n        }, completeHandler: { () -\u003e Void in\n            NSLog(\"Finished caching\")\n        }, failureHandler: { (error) -\u003e Void in\n            NSLog(\"Error caching: %@\", error)\n    })\n}\n```\n\nOnce cached, you can simply load the webpage in a UIWebView and it will be loaded from the Mattress cache, like magic.\n\n**Example**\n-----------\n\nTo run the example, please use the following steps.\n\n1. Open the Mattress workspace\n2. Run the MattressExample scheme\n3. Tap \"Cache Page\"\n4. Wait for the log message stating caching has finished\n5. Disable internet access\n6. Tap \"Load Page\" - the page will load via Mattress\n \n**Considerations**\n---------\n\nMattress does not work with WKWebView. The current WKWebView implementation uses its own internal system for caching and does not properly integrate with NSURLProtocol to allow Mattress to intercept requests made.\n\nDue to Mattress' current architecture and use of web views, a good chunk of the caching work must happen on the main thread. This is obviously not a problem when caching pages while in the background, such as during a background fetch. However, it is something to be mindful of when your app is active in the foreground. We have had good luck using it this way with minimal performance impact, but your mileage may vary.\n\n\n**Contributing**\n----------------\n\nContributions are welcome. Please feel free to open a pull request. \n\nWe also welcome feature requests and bug reports. Just open an issue.\n\n**License**\n---------\n\nMattress is licensed under the MIT License.\n","funding_links":[],"categories":["Cache","HarmonyOS","Swift"],"sub_categories":["Windows Manager","Other free courses","Getting Started"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbuzzfeed%2Fmattress","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbuzzfeed%2Fmattress","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbuzzfeed%2Fmattress/lists"}