gpt4 book ai didi

ios - 是否会阻止具有响应 header `Cache-Control:Private` 的文件缓存在 NSURLCache 中?

转载 作者:可可西里 更新时间:2023-11-01 06:06:20 25 4
gpt4 key购买 nike

是否会阻止带有响应 header Cache-Control:Private 的文件缓存在 NSURLCache 中?共享缓存(如 setSharedCacheNSURLCache.sharedCache() )或自定义缓存?

为了扩展,我有 UIWebView,我需要在离线时访问它。此 WebView 的源有多个与之关联的外部 CSS 和 JS 文件。我可以缓存大部分网站(CSS 等看起来就位),但它似乎没有缓存为网站提供重要信息的特定 JavaScript 文件。我注意到不会缓存的文件与其余文件之间的区别是它的 Cache-Control 设置为私有(private)(其他是公共(public)的)。但是,根据我的阅读,将缓存控制设置为私有(private)是为了防止在代理上进行缓存。它会影响 iOS 上的缓存吗?

设置缓存

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

let URLCache: NSURLCache = NSURLCache(memoryCapacity: 10 * 1024 * 1024,
diskCapacity: 50 * 1024 * 1024,
diskPath: nil)

NSURLCache.setSharedURLCache(URLCache)

println("Disk cache usage: \(NSURLCache.sharedURLCache().currentDiskUsage)")

// http://stackoverflow.com/questions/21957378/how-to-cache-using-nsurlsession-and-nsurlcache-not-working
sleep(1)

return true
}

使用缓存

func getWebPage(onCompletion: (NSString, NSURL) -> Void) {

let url = getApplicationSelectorURL()

let request = NSURLRequest(URL: url, cachePolicy: .ReturnCacheDataElseLoad, timeoutInterval: 10.0)

let queue = NSOperationQueue()

NSURLConnection.sendAsynchronousRequest(request, queue: queue, completionHandler: { response, data, error in
println("Web page task completed")

var cachedResponse: NSCachedURLResponse

if (error != nil) {
println("NSURLConnection error: \(error.localizedDescription)")
if let cachedResponse = NSURLCache.sharedURLCache().cachedResponseForRequest(request) {
if let htmlString = NSString(data: cachedResponse.data, encoding: NSUTF8StringEncoding) {
onCompletion(htmlString, url)
} else {
println("htmlString nil")
}
} else {
println("cacheResponse nil")
}
} else {
cachedResponse = NSCachedURLResponse(response: response, data: data, userInfo: nil, storagePolicy: .Allowed)
NSURLCache.sharedURLCache().storeCachedResponse(cachedResponse, forRequest: request)
if let htmlString = NSString(data: data, encoding: NSUTF8StringEncoding) {
onCompletion(htmlString, url)
} else {
println("htmlString nil")
}
}
})
}

填充 UIWebView

APICommunicator.sharedInstance.getWebPage({ htmlString, url in

dispatch_async(dispatch_get_main_queue(),{
self.webView.loadHTMLString(htmlString, baseURL: url)
})

})

最佳答案

我最终创建了一个类似于 NSURLConnectionDelegate 方法 willCacheResponse 的方法,并替换了 Cache-Control:private header 。

willCacheResponse 方法

func willCacheResponse(cachedResponse: NSCachedURLResponse) -> NSCachedURLResponse?
{
let response = cachedResponse.response

let HTTPresponse: NSHTTPURLResponse = response as NSHTTPURLResponse

let headers: NSDictionary = HTTPresponse.allHeaderFields

var modifiedHeaders: NSMutableDictionary = headers.mutableCopy() as NSMutableDictionary

modifiedHeaders["Cache-Control"] = "max-age=604800"

let modifiedResponse: NSHTTPURLResponse = NSHTTPURLResponse(
URL: HTTPresponse.URL!,
statusCode: HTTPresponse.statusCode,
HTTPVersion: "HTTP/1.1",
headerFields: modifiedHeaders)!

let modifiedCachedResponse = NSCachedURLResponse(
response: modifiedResponse,
data: cachedResponse.data,
userInfo: cachedResponse.userInfo,
storagePolicy: cachedResponse.storagePolicy)

return modifiedCachedResponse
}

调用方式

if let cachedResponse = self.willCacheResponse(
NSCachedURLResponse(response: response,
data: data,
userInfo: nil,
storagePolicy: .Allowed)) {

NSURLCache.sharedURLCache().storeCachedResponse(cachedResponse, forRequest: request)
}

现在它可以在离线时正确显示。多么美妙的旅程啊。

关于ios - 是否会阻止具有响应 header `Cache-Control:Private` 的文件缓存在 NSURLCache 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28922841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com