Skip to content

Commit 527b854

Browse files
committed
fallback to raw file without using NSKeyedUnarchiver
1 parent 0732cf0 commit 527b854

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

EVURLCache.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Pod::Spec.new do |s|
88
#
99

1010
s.name = "EVURLCache"
11-
s.version = "3.3.1"
11+
s.version = "3.5.0"
1212
s.summary = "NSURLCache subclass for handeling all web requests that use NSURLRequest"
1313
s.description = "This is a NSURLCache subclass for handeling all web requests that use NSURLRequest. (This includes UIWebView)"
1414
s.homepage = "https://github.com/evermeer/EVURLCache"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

EVURLCache/Pod/EVURLCache.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,31 @@ open class EVURLCache: URLCache {
168168
// This works for the game, but not for my site.
169169
return response
170170
} else {
171-
EVURLCache.debugLog("The file is probably not put in the local path using NSKeyedArchiver \(storagePath)")
171+
// File was not written using the NSKeyedUnarchiver. Will return the raw file as the cache response
172+
if let content:NSData = NSData(contentsOfFile: storagePath) {
173+
let mimeType = getMimeType(storagePath)
174+
let response = URLResponse(url: url, mimeType: mimeType, expectedContentLength: content.length, textEncodingName: nil)
175+
EVURLCache.debugLog("CACHE returning cache response: mimeType = \(mimeType), path = \(storagePath)");
176+
return CachedURLResponse(response: response, data: content as Data)
177+
}
178+
EVURLCache.debugLog("CACHE could not be read from \(storagePath)")
172179
}
173180
return nil
174181
}
175182

183+
// Make sure the correct mimetype is returned from the cache
184+
private static func getMimeType(_ path: String) -> String {
185+
var mimeType = "text/html"
186+
if let fileExtension: String = path.components(separatedBy: ".").last {
187+
if let uti: CFString = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension as NSString, nil)?.takeRetainedValue() {
188+
if let type = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType)?.takeRetainedValue() {
189+
mimeType = type as String
190+
}
191+
}
192+
}
193+
return mimeType
194+
}
195+
176196
// Will be called by NSURLConnection when a request is complete.
177197
open override func storeCachedResponse(_ cachedResponse: CachedURLResponse, for request: URLRequest) {
178198

0 commit comments

Comments
 (0)