viewDidLoad里边代码如下:_urlStr为网页链接
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES) objectAtIndex:0];
NSString * path = [cachesPathstringByAppendingString:[NSStringstringWithFormat:@"/Caches/%lu.html",(unsignedlong)[_urlStrhash]]];
NSString *htmlString = [NSStringstringWithContentsOfFile:pathencoding:NSUTF8StringEncodingerror:nil];
if (!(htmlString ==nil || [htmlString isEqualToString:@""])) {
[_webViewloadHTMLString:htmlStringbaseURL:[NSURLURLWithString:_urlStr]];
}else{
NSURL *url = [NSURLURLWithString:_urlStr];
NSURLRequest *request = [NSURLRequestrequestWithURL:url];
[_webViewloadRequest:request];
[selfwriteToCache];
}
/**
* 网页缓存写入文件
*/
- (void)writeToCache
{
NSString * htmlResponseStr = [NSStringstringWithContentsOfURL:[NSURLURLWithString:_urlStr]encoding:NSUTF8StringEncodingerror:Nil];
//创建文件管理器
NSFileManager *fileManager = [[NSFileManageralloc]init];
//获取document路径
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
[fileManager createDirectoryAtPath:[cachesPathstringByAppendingString:@"/Caches"]withIntermediateDirectories:YESattributes:nilerror:nil];
//写入路径
NSString * path = [cachesPathstringByAppendingString:[NSStringstringWithFormat:@"/Caches/%lu.html",(unsignedlong)[_urlStrhash]]];
[htmlResponseStr writeToFile:pathatomically:YESencoding:NSUTF8StringEncodingerror:nil];
}