如何在ios uiwebvieww中使用缓存

如何在UIWebView中使用缓存_百度知道
如何在UIWebView中使用缓存
提问者采纳
1.HTML5 , Manifest始我想使用HTML5离线存储功能析Manifest文件存储更新部资源文件经实践发现UIWebView根本支持HTML5实现Webkit页面渲染部所要实现缓存必须要另辟蹊径2.NSURLCache
尽管官说明文档面说NSURLCacheNSCachedURLResponse用于缓存经我测试像仅仅能用于加载本某些资源文件(篇博客原文英文翻译)且限制(像根据iphone版本同同25KB吧)比图片JS代码 于整体页面进行加载且经测试没觉加载速度明显提高我用缓存策略NSURLRequestReturnCacheDataElseLoad(能没读取本缓存文件)离线模式加载(能baseURL关系)找篇博客种新解决思路经我测试实现缓存另外做点引申于态获取数据页面我需要缓存些请求要滤掉先新建文件所需要缓存请求URL写文件象HTML5 Cache Manifest需要使用缓存候读取文件并重写- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request
内请求进行判断属于文件内比web service请求直接返其继续处理3.ASIHTTPRequestASIDownloadCache
ASIWebPageRequest
首先我说确实框架使用起确实便于缓存问题像跟第二点提效差加载速度没明显提升离线模式加载实现代码:-(void)loadURL:(NSURL*)url{
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
//ASIWebPageRequest *request= [ASIWebPageRequest requestWithURL:url];
[request setDelegate:self];
//[request setUrlReplacementMode:ASIReplaceExternalResourcesWithData];
[request setDidFailSelector:@selector(webPageFetchFailed:)];
[request setDidFinishSelector:@selector(webPageFetchSucceeded:)];
//设置缓存
[request setDownloadCache:[ASIDownloadCache sharedCache]];
//[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[request setCachePolicy:ASIAskServerIfModifiedWhenStaleCachePolicy|ASIFallbackToCacheIfLoadFailsCachePolicy];
[request setDownloadDestinationPath:[[ASIDownloadCache sharedCache]pathToStoreCachedResponseDataForRequest:request]];
[request startAsynchronous];}- (void)webPageFetchFailed:(ASIHTTPRequest *)theRequest{
// Obviously you should handle the error properly...
NSLog(@&%@&,[theRequest error]);
NSString *path = [[NSBundle mainBundle] pathForResource:@&error1.html& ofType:nil inDirectory:@&WebResources/Error&];
*url=[NSURL fileURLWithPath:path];
[viewer loadRequest:[NSURLRequest requestWithURL:url]];}- (void)webPageFetchSucceeded:(ASIHTTPRequest *)theRequest{
NSString *response = [NSString stringWithContentsOfFile:
[theRequest downloadDestinationPath] encoding:[theRequest responseEncoding] error:nil];
// Note we're setting the baseURL to the url of the page we downloaded. This is important!
[viewer loadHTMLString:response baseURL:[theRequest url]];
//[viewer loadHTMLString:response baseURL:nil];}
其他类似问题
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁& &&//清除UIWebView的缓存
& & [ [NSURLCache&
sharedURLCache ] removeAllCachedResponses];
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:701256次
积分:8970
积分:8970
排名:第659名
原创:236篇
转载:92篇
评论:123条
(3)(4)(6)(1)(2)(2)(2)(2)(2)(2)(4)(2)(8)(16)(10)(23)(14)(7)(16)(4)(3)(3)(15)(3)(42)(6)(17)(16)(1)(4)(7)(6)(9)(17)(1)(1)(5)(2)(6)(1)(3)(1)(9)(7)(2)(1)(4)(5)(7)动态加载在UIWebView中与本地缓存不工作的javascript文件
我试图写一个应用程序中的很多java脚本的一个UIWebView里面。这个Java脚本和动态加载(使用jQuery),在需要时。
在与jQuery的文件被加载为预期,但没有code.js文件。该code.js文件是这样的请求(剪断从Java脚本代码):
function require(moduleName,filePath) {
var uri = fileP
jQuery.ajax({
type: "GET",
async: false,
dataType: "script",
success: function(content) {
console.log("Receive content of "+moduleName);
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log("Error content of "+moduleName);
console.log("Status: " + textStatus);
console.log("Thrown error: " + errorThrown);
console.log("h" + XMLHttpRequest.getAllResponseHeaders());
console.log(XMLHttpRequest.responseText);
function start() {
require("test", "code.js");
在start()函数被调用的body标签的onload事件。该code.js文件只包含以下三行代码:
alert("Loaded file syncronosly");
alert("second");
我从这段代码得到的输出看起来像这样(我有额外的js代码,转发的console.log调用到Xcode的控制台,我省略):
UIWebView console: Error content of test
UIWebView console: Status: error
UIWebView console: Thrown error:
UIWebView console: h
UIWebView console: HTTP Error (0 error).
UIWebView console: alert("Loaded file syncronosly");
alert("second");
我只要我试图返回code.js在重写的NSURLCache类的内容得到这个行为。到底想法是有其它的一部分运行在一个UIWebView,而不需要从外部Web服务器加载的内容的应用程序。
这是缓存类(LocalSubstitutionCache)的缩短码:
NSString *pathString = [[request URL] absoluteString];
NSLog(@"request =& %@", pathString);
NSString* fileToLoad =
if ([pathString isEqualToString:@" CodeGo.net
fileToLoad = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
else if ([pathString hasPrefix:@" CodeGo.net
fileToLoad = [[NSBundle mainBundle] pathForResource:@"code" ofType:@"js"];
else if ([pathString isEqualToString:@" CodeGo.net
fileToLoad = [[NSBundle mainBundle] pathForResource:@"jquery-1.6.2.min" ofType:@"js"];
if (!fileToLoad) {
// No local data needed for this request let super handle it:
return [super cachedResponseForRequest:request];
NSData *data = [NSData dataWithContentsOfFile:fileToLoad];
NSURLResponse *response =
[[NSURLResponse alloc]
initWithURL:[request URL]
MIMEType:[self mimeTypeForPath:pathString]
expectedContentLength:[data length]
textEncodingName:nil];
cachedResponse =
[[NSCachedURLResponse alloc] initWithResponse:response data:data];
(缓存代码是从这里:
现在,在我的视图控制器(其中包含一个web视图)我做到以下几点:
- (void)viewDidLoad {
[super viewDidLoad];
// This line loads the content with the cache enabled (and does not work):
[_webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@" CodeGo.net
现在 CodeGo.net,有趣的是,当我替换代码中使用下面的代码:
- (void) viewDidLoad {
[super viewDidLoad];
// This two line load the content without the cache:
NSString* fileToLoad = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
[_webview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:fileToLoad]]];
现在,一切都工作正常,我也得到显示我的两个警报的。所以我怀疑是什么我'返回在缓存不是因为当初请求返回时,它是渔获缓存。
以下是我已经检查了:
文件URL的工作情况。我原来的代码从web服务器获取不进行缓存的代码。但对于示例项目我的文件的URL,因为这使项目更加简单。
内容类型返回。是在这两种情况下的文本/ javascript中
特价商品标题设置。我已经检查了HTTP代理什么的标题,这是不缓存请求被置。有没有设置特殊的头。
我最大的问题是,jQuery的错误回调不回我只是得到错误的输出
你可以从这里下载示例项目:
希望能在这里
本文地址 :CodeGo.net/417188/
-------------------------------------------------------------------------------------------------------------------------
1. 经过以上工作,我找到了解决我的问题。并回答这里我发现了什么。
该解决方案是创建一个自定义NSURLProtocol并在应用程序中处理HTTP协议注册此。代码看起来像这样(我省略了错误处理代码):
#import "MyHttpURLProtocol.h"
@implementation MyHttpURLProtocol
+ (BOOL)canInitWithRequest:(NSURLRequest *)request {
return [[[request URL] scheme] isEqualToString:@"http"]; // we handle http requests
+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request {
- (void) startLoading {
id&NSURLProtocolClient& client = [self client];
NSURLRequest* request = [self request];
NSString *pathString = [[request URL] absoluteString];
NSString* fileToLoad =
if ([pathString isEqualToString:@" CodeGo.net
fileToLoad = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
else if ([pathString hasPrefix:@" CodeGo.net
fileToLoad = [[NSBundle mainBundle] pathForResource:@"code" ofType:@"js"];
else if ([pathString isEqualToString:@" CodeGo.net
fileToLoad = [[NSBundle mainBundle] pathForResource:@"jquery-1.6.2.min" ofType:@"js"];
NSData* data = [NSData dataWithContentsOfFile:fileToLoad];
NSHTTPURLResponse* response = [[NSHTTPURLResponse alloc] initWithURL:[request URL] statusCode:200 HTTPVersion:@"HTTP/1.1" headerFields:[NSDictionary dictionary]];
[client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
[client URLProtocol:self didLoadData:data];
[client URLProtocolDidFinishLoading:self];
- (void) stopLoading {}
现在这个代码允许加载该文件被访问下面的代码:
[_webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@" CodeGo.net
而且不要忘了注册自定义的协议在你的应用程序委托:
[NSURLProtocol的registerClass:[TOHttpURLProtocol类]];
[NSURLProtocol registerClass:[MyHttpURLProtocol class]];
令人惊讶的是简单的解决方案可以是你以后找到它。这里是链接到更新的示例项目:
和清酒这里是链接到博客文章而找到的解决办法:
本文标题 :动态加载在UIWebView中与本地缓存不工作的javascript文件
本文地址 :CodeGo.net/417188/
Copyright (C) 2014 CodeGo.net 沪ICP备号 联系电邮: (#=@)4225人阅读
UIWebView清除Cookie:
//清除cookies
NSHTTPCookie *
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies])
[storage deleteCookie:cookie];
UIWebView清除缓存:
//清除UIWebView的缓存
[[NSURLCachesharedURLCache] removeAllCachedResponses];
(By ItNoob.Matrix 链接 /ItNoob)
分类:&&1013人阅读&&&
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:230480次
积分:3273
积分:3273
排名:第3854名
原创:66篇
转载:244篇
评论:19条
(3)(6)(1)(1)(30)(4)(2)(13)(10)(6)(3)(11)(3)(18)(3)(12)(5)(4)(16)(14)(26)(8)(6)(22)(14)(10)(45)(3)(7)(1)(1)(3)

我要回帖

更多关于 ios uiwebview 的文章

 

随机推荐