我正在尝试使用来自NSURLCache的数据加载UIWebView Offline。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // setup cache int cacheSizeMemory = 4*1024*1024; // 4MB int cacheSizeDisk = 32*1024*1024; // 32MB NSURLCache *urlCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"]; [NSURLCache setSharedURLCache:urlCache]; } -(void)loadWebView{ request = [NSURLRequest requestWithURL:myURL cachePolicy: NSURLRequestReturnCacheDataElseLoad timeoutInterval:1000]; [self.webView loadRequest:request]; }
当我尝试离线加载UIWebView时,它调用didFailWithError而不是connectionDidFinishLoading。然而,我的缓存目录包含离线数据和资源,如css,js文件,但UIWebView无法访问它。
我已通过覆盖以下方法确认了这一点:
-(NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request { NSCachedURLResponse *response = [super cachedResponseForRequest:request]; if([response.data length]> 0){ // this is called for all css , js script files NSLog(@"Returning data from cache"); } return response; }
此外,从缓存UIWebview返回的数据无法加载它。