如何正确地快速缓存NSURLRequest?

Joh*_*son 2 xcode caching uiwebview ios swift

原谅是否已被问到,但我已经进行了大量搜索,但似乎无法迅速找到我想要的答案。我有一个UIWebView可以加载PDF文件的工具,如果他们退出WebView并返回,我不希望用户再次经历繁琐的加载过程,那么如何缓存请求以加快加载速度?

这是我的代码:

var contentUrlPassedOn: String!

    override func viewDidLoad() {
        super.viewDidLoad()

        myWebView.delegate = self

        let url: NSURL! = NSURL(string: contentUrlPassedOn)

        myWebView.loadRequest(NSURLRequest(URL: url))
        var request: NSURLRequest
}
Run Code Online (Sandbox Code Playgroud)

更新

我要完成的任务可以在此视频中看到

这是Dropbox的iOS应用示例。当已经加载了一个文件时,进入该文件的所有其他行程(在本例中为PDF)将变得快速而简单。此方法正是我要复制的方法。

有什么建议么?

Leo*_*Leo 5

GIF

  1. 第一次输入此ViewController时,您可以看到下载进度
  2. 然后第二次,它只加载本地文件。因此非常快

和代码

class ViewController: UIViewController,NSURLSessionDownloadDelegate{
var sesson:NSURLSession!
var webview:UIWebView!
var progressView:UIProgressView!
override func viewDidLoad() {
    super.viewDidLoad()
    self.sesson =   NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration(), delegate: self, delegateQueue: NSOperationQueue.mainQueue())
    self.webview = UIWebView(frame: self.view.frame)
    self.view.addSubview(webview)
    let documentDir = NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask).first as! NSURL
    let pdfFilePath = documentDir.URLByAppendingPathComponent("Test.pdf");
    if NSFileManager.defaultManager().fileExistsAtPath(pdfFilePath.path!){
        let request = NSURLRequest(URL: pdfFilePath)
        webview.loadRequest(request)
    }else{
        progressView = UIProgressView(progressViewStyle: UIProgressViewStyle.Default);
        progressView.frame = CGRectMake(0, 0,200, 4)
        progressView.progress = 0
        progressView.progressTintColor = UIColor.blueColor()
        progressView.center = self.view.center
        self.view.addSubview(progressView)
        let remoteURL = "https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/URLLoadingSystem/URLLoadingSystem.pdf"
        let request = NSURLRequest(URL: NSURL(string: remoteURL)!)
        let downloadTask = self.sesson.downloadTaskWithRequest(request)
        downloadTask.resume()
    }
    // Do any additional setup after loading the view, typically from a nib.
}
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {
    if progressView != nil{
        progressView.removeFromSuperview()
    }
    let documentDir = NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask).first as! NSURL
    let pdfFilePath = documentDir.URLByAppendingPathComponent("Test.pdf");
    NSFileManager.defaultManager().moveItemAtURL(location, toURL: pdfFilePath, error: nil)
    let request = NSURLRequest(URL: pdfFilePath)
    self.webview.loadRequest(request)
}
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
    var curprogress = Float(totalBytesWritten)/Float(totalBytesExpectedToWrite)
    progressView.progress = curprogress
}
}
Run Code Online (Sandbox Code Playgroud)