Skip to content

Commit

Permalink
cache complete NSCachedURLResponse instead of only the data
Browse files Browse the repository at this point in the history
  • Loading branch information
evermeer committed Dec 22, 2015
1 parent 3f5ec8e commit 33be491
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 12 deletions.
2 changes: 1 addition & 1 deletion EVURLCache.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Pod::Spec.new do |s|
#

s.name = "EVURLCache"
s.version = "2.1.5"
s.version = "2.2.0"
s.summary = "NSURLCache subclass for handeling all web requests that use NSURLRequest"
s.description = "This is a NSURLCache subclass for handeling all web requests that use NSURLRequest. (This includes UIWebView)"
s.homepage = "https://github.com/evermeer/EVURLCache"
Expand Down
16 changes: 6 additions & 10 deletions EVURLCache/Pod/EVURLCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,11 @@ public class EVURLCache : NSURLCache {
} catch {}
}


// Return the cache response
if let content:NSData = NSData(contentsOfFile: storagePath) {
let mimeType = getMimeType(storagePath)
let response = NSURLResponse(URL: request.URL!, MIMEType: mimeType, expectedContentLength: content.length, textEncodingName: nil)
EVURLCache.debugLog("CACHE returning cache response: mimeType = \(mimeType), path = \(storagePath)");
return NSCachedURLResponse(response: response, data: content)
}
EVURLCache.debugLog("CACHE could not be read from \(storagePath)");
// Read object from file
if let response = NSKeyedUnarchiver.unarchiveObjectWithFile(storagePath) as? NSCachedURLResponse {
EVURLCache.debugLog("Returning cached data from \(storagePath)");
return response
}
return nil
}

Expand Down Expand Up @@ -160,7 +156,7 @@ public class EVURLCache : NSURLCache {

// save file
EVURLCache.debugLog("Writing data to \(storagePath)");
if !cachedResponse.data.writeToFile(storagePath, atomically: true) {
if !NSKeyedArchiver.archiveRootObject(cachedResponse, toFile: storagePath) {
EVURLCache.debugLog("Could not write file to cache");
} else {
EVURLCache.debugLog("CACHE save file to Cache : \(storagePath)");
Expand Down
2 changes: 1 addition & 1 deletion EVURLCache/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ViewController: UIViewController {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

if let url = NSURL(string: "http://evict.nl") {
if let url = NSURL(string: "http://game.zorropk.com/gamenow/xiao5haiyanglixianji/") {
webView.loadRequest(NSURLRequest(URL: url))
}
}
Expand Down

0 comments on commit 33be491

Please sign in to comment.