ios – 是否会阻止在NSURLCache中缓存带有响应头“Cache-Control:Private”的文件?

ios – 是否会阻止在NSURLCache中缓存带有响应头“Cache-Control:Private”的文件?,第1张

概述是否会阻止在NSURLCache中缓存具有响应头Cache-Control:Private的文件?共享缓存(如setSharedCache和NSURLCache.sharedCache())还是自定义缓存? 为了扩展,我有离线时需要访问的UIWebView.此WebView的源代码具有多个与之关联的外部CSS和JS文件.我可以缓存网站的大部分内容(CSS等等到位),但它似乎没有缓存为网站提供重要信 是否会阻止在NSURLCache中缓存具有响应头Cache-Control:Private的文件?共享缓存(如setSharedCache和NSURLCache.sharedCache())还是自定义缓存?

为了扩展,我有离线时需要访问的UIWebVIEw.此WebVIEw的源代码具有多个与之关联的外部CSS和Js文件.我可以缓存网站的大部分内容(CSS等等到位),但它似乎没有缓存为网站提供重要信息的特定JavaScript文件.我在不缓存的文件与其余文件之间注意到的差异是它的Cache-Control设置为private(其他是公共的).但是,根据我的阅读,将缓存控件设置为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)")    // https://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头.

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,storagePolicy: .Allowed)) {    NSURLCache.sharedURLCache().storeCachedResponse(cachedResponse,forRequest: request)}

现在它在离线时正确显示.真是个旅程.

总结

以上是内存溢出为你收集整理的ios – 是否会阻止在NSURLCache中缓存带有响应头“Cache-Control:Private”的文件?全部内容,希望文章能够帮你解决ios – 是否会阻止在NSURLCache中缓存带有响应头“Cache-Control:Private”的文件?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1025994.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存