Mattress 1.0.3

Mattress 1.0.3

测试测试
语言语言 SwiftSwift
许可证 MIT
发布上次发布2016年1月
SPM支持 SPM

Kevin Lord 维护。



Mattress 1.0.3

Mattress

一个将整个网页存储到独立于但不与标准 NSURLCache 层互操作的磁盘缓存的 Swift 框架。这对于预缓存网页以加速加载,以及在离线浏览时提供网页内容都非常有用。

要求

  • iOS 7.0+ (集成为嵌入式框架需要 iOS 8)

安装

Mattress 包含一个围绕 CommonCrypto 的包装,以便它能轻松地从 Swift 中使用。您需要确保将 Mattress 和 CommonCrypto 框架都包含在您的项目中。

Carthage (推荐)

如果您还没有安装 Carthage,您需要使用 Homebrew 安装它。

$ brew update
$ brew install carthage

安装后,将其添加到您的 Cartfile 中

github "buzzfeed/Mattress" >= 1.0.0

然后您需要使用 Carthage 构建,并手动将 Mattress 和 CommonCrypto 框架集成到您的项目中。

$ carthage build

CocoaPods

如果您还没有使用 CocoaPods,您需要使用 RubyGems 安装它。

$ gem install cocoapods

安装后,将其添加到您的 Podfile 中

pod 'Mattress', '~> 1.0.0'

手动

  1. 打开 Mattress 文件夹,并将 Mattress.xcodeproj 拖放到您的应用程序项目文件导航器中。注意:Mattress 项目需要添加到目标项目下的某个地方,否则您无法将其添加为目标依赖项。
  2. 确保 Mattress 项目的部署目标与应用程序目标匹配。
  3. 在您的目标“构建阶段”面板中,将 Mattress.framework 添加到“目标依赖项”
  4. 点击面板左上角的 + 按钮,选择“新复制文件阶段”。将这个新阶段重命名为“复制框架”,将“目标”设置为“框架”,并添加 Mattress.frameworkCommonCrypto.framework

用法

您应该在应用程序的 application:didFinishLaunching: 方法中创建 URLCache 实例并将其设置为应用程序的共享缓存。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    let kB = 1024
    let MB = 1024 * kB
    let GB = 1024 * MB
    let isOfflineHandler: (() -> Bool) = {
        // This is for demonstration only.
        // You should use Reachability or your preferred method to determine offline status
        // and return a proper value here.
        return false
    }
    let urlCache = Mattress.URLCache(memoryCapacity: 20 * MB, diskCapacity: 20 * MB, diskPath: nil,
        mattressDiskCapacity: 1 * GB, mattressDiskPath: nil, mattressSearchPathDirectory: .DocumentDirectory,
        isOfflineHandler: isOfflineHandler)

    NSURLCache.setSharedURLCache(urlCache)
    return true
}

要将网页缓存到 Mattress 磁盘缓存中,只需调用 URLCache 的 diskCacheURL:loadedHandler: 方法。

NSLog("Caching page")
let urlToCache = NSURL(string: "https://www.google.com")
if let
    cache = NSURLCache.sharedURLCache() as? Mattress.URLCache,
    urlToCache = urlToCache
{
    cache.diskCacheURL(urlToCache, loadedHandler: { (webView) -> (Bool) in
            /*
               Note: The below code should work for fairly simple pages. However, if the page you are
               attempting to cache contains progressively / lazily loaded images or other assets you
               will also need to trigger any JavaScript functions here to mimic user actions and
               ensure those assets are also loaded before returning true.
            */
            let state = webView.stringByEvaluatingJavaScriptFromString("document.readyState")
            if state == "complete" {
                // Loading is done once we've returned true
                return true
            }
            return false
        }, completeHandler: { () -> Void in
            NSLog("Finished caching")
        }, failureHandler: { (error) -> Void in
            NSLog("Error caching: %@", error)
    })
}

一旦缓存,您只需在 UIWebView 中加载网页,它就会从 Mattress 缓存中加载,如同魔法一般。

示例

要运行示例,请按照以下步骤进行。

  1. 打开 Mattress 工作空间
  2. 运行 MattressExample 计划
  3. 点击“缓存页面”
  4. 等待日志消息表明缓存已完成
  5. 禁用互联网访问
  6. 轻触“加载页面”——页面将通过Mattress加载

注意事项

Mattress不能与WKWebView一起使用。当前的WKWebView实现使用它自己的内部系统进行缓存,并且没有正确地与NSURLProtocol集成,以允许Mattress截获发出的请求。

由于Mattress当前的架构和web视图的使用,大部分缓存工作必须在主线程上完成。这在后台加载页面时显然不是问题,例如在后台获取期间。然而,当应用程序在前景中活跃时,这需要留心。我们已经成功地以这种方式使用它,并且性能影响最小,但您的结果可能会有所不同。

贡献

欢迎贡献。请随时提交一个pull request。

我们也欢迎功能请求和错误报告。只需打开一个问题。

许可

Mattress使用MIT许可证。