ios - 如何使用 EVURLCache 在 PreCache 中加载 UIWebView

标签 ios swift caching

我正在尝试创建一个具有 UIWebview 的 iOS 应用程序,我想在其中加载网站。我尝试使用 EVURLCache 进行缓存。但它不会从我项目中的 PreCache 文件夹加载。

该应用程序第一次运行良好。但是当我尝试再次运行时,它只显示空白屏幕。我认为该应用程序尝试从缓存中加载页面,但该应用程序无法找到它的缓存..

我使用 https://github.com/evermeer/EVURLCache用于缓存。我通过 pod 安装

主VC

import UIKit

class MainVC: UIViewController, UIWebViewDelegate {
    @IBOutlet weak var webView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        webView.delegate = self

        let url = URL(string: BASE_URL)
        let request = URLRequest(url: url!)

        webView.loadRequest(request)
    }


    func webViewDidStartLoad(_ webView: UIWebView) {
        print("*****************")
        print(" webViewDidStartLoad(")
        print("*****************")
    }

    func webViewDidFinishLoad(_ webView: UIWebView) {
        print("*****************")
        print("webViewDidFinishLoad")
        print("*****************")
    }

}

应用委托(delegate)

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        EVURLCache.LOGGING = true // We want to see all caching actions
        EVURLCache.MAX_FILE_SIZE = 26 // We want more than the default: 2^26 = 64MB
        EVURLCache.MAX_CACHE_SIZE = 30 // We want more than the default: 2^30 = 1GB
        EVURLCache.activate()

        return true
    }

然后我找到了缓存文件夹

04/10/2017 18:01:41:952 FedNet)[1696:.] EVURLCache.swift(245) storeCachedResponse(_:for:):
    CACHE save file to Cache  : /Users/apple/Library/Developer/CoreSimulator/Devices/BF935382-BD49-4F37-85A1-C3FA0E1B094B/data/Containers/Data/Application/AF33E59E-1BB4-46CC-9236-BEB42F53D606/Documents/Cache/www.fednetbank.com/corp/l001/consumer/theme/login/img/banner.jpg

enter image description here

当我使用 PreCache 文件夹运行应用程序时,它给我一个空白屏幕....

最佳答案

这可能是由重定向引起的。重定向不会被缓存。我试图用一个协议(protocol)来解决这个问题,但我已经注意到它有时会失败。您能否尝试从 EVURLCache 中删除以下行,然后重试?

URLProtocol.registerClass(EVURLProtocol.self)

关于ios - 如何使用 EVURLCache 在 PreCache 中加载 UIWebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43325780/

相关文章:

javascript - 如何强制客户端刷新 JavaScript 文件?

asp.net - iisExpress 本地 httpruntime.cache 始终为 null

ios - 使用iwatch快速提取心率代码

ios - (xcode) 我想长按一个按钮导致键盘弹出

ios - 无需 iCloud 登录的 CloudKit 公共(public)数据库读取

arrays - 如何创建元组数组?

ios - 从另一个 TableView 内的 TableView 访问按钮索引

C# 代码优化、分析、应用程序优化

iphone - 使用适用于 iOS 的 Dropbox Chooser SDK 时无法生成链接错误

ios - 使用 layoutSubviews 与 drawRect 自定义 UIView 绘图