Skip to content

Commit

Permalink
fallback to raw file without using NSKeyedUnarchiver
Browse files Browse the repository at this point in the history
  • Loading branch information
evermeer committed Jul 4, 2018
1 parent 0732cf0 commit 527b854
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 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 = "3.3.1"
s.version = "3.5.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
8 changes: 8 additions & 0 deletions EVURLCache.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
22 changes: 21 additions & 1 deletion EVURLCache/Pod/EVURLCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,31 @@ open class EVURLCache: URLCache {
// This works for the game, but not for my site.
return response
} else {
EVURLCache.debugLog("The file is probably not put in the local path using NSKeyedArchiver \(storagePath)")
// File was not written using the NSKeyedUnarchiver. Will return the raw file as the cache response
if let content:NSData = NSData(contentsOfFile: storagePath) {
let mimeType = getMimeType(storagePath)
let response = URLResponse(url: url, mimeType: mimeType, expectedContentLength: content.length, textEncodingName: nil)
EVURLCache.debugLog("CACHE returning cache response: mimeType = \(mimeType), path = \(storagePath)");
return CachedURLResponse(response: response, data: content as Data)
}
EVURLCache.debugLog("CACHE could not be read from \(storagePath)")
}
return nil
}

// Make sure the correct mimetype is returned from the cache
private static func getMimeType(_ path: String) -> String {
var mimeType = "text/html"
if let fileExtension: String = path.components(separatedBy: ".").last {
if let uti: CFString = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension as NSString, nil)?.takeRetainedValue() {
if let type = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType)?.takeRetainedValue() {
mimeType = type as String
}
}
}
return mimeType
}

// Will be called by NSURLConnection when a request is complete.
open override func storeCachedResponse(_ cachedResponse: CachedURLResponse, for request: URLRequest) {

Expand Down

0 comments on commit 527b854

Please sign in to comment.