UIwebView implements html offline cache and uiwebview offline Cache
1. the html cache mainly adopts the ASIHTTPRequest Cache Policy.
(1) set cache policies
// Set the cache ASIDownloadCache * cache = [[ASIDownloadCache alloc] init]; self. myCache = cache; // set the cache path NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NSString * documentDirectory = [paths objectAtIndex: 0]; // set the cache storage path [self. myCache setStoragePath: [documentDirectory stringByAppendingPathComponent: @ "resource"]; // ASIAskServerIfModifiedCachePolicy is roughly the same as the default cache. The difference is that The request will go to the server to determine whether there is an update // ASIOnlyLoadIfNotCachedCachePolicy. If there is a slow local connection, the/ASIFallbackToCacheIfLoadFailsCachePolicy option will always be used in combination with other options. If the request fails, if a cache exists, the local cache information is returned. [self. myCache setDefaultCachePolicy: ASIFallbackToCacheIfLoadFailsCachePolicy]; // sets the cache policy.
(2) Set asynchronous Cache
NSURL * Requesturl = [NSURL URLWithString: url]; ASIHTTPRequest * request = [ASIHTTPRequest requestWithURL: Requesturl]; /// obtain the global variable AppDelegate * appDelegate = [[UIApplication sharedApplication] delegate]; /// set the cache mode [request setDownloadCache: appDelegate. myCache]; // set the cache data storage policy. Here, if there is no update or the cache data cannot be accessed online, read the cache data [request setCacheStoragePolicy: ASICachePermanentlyCacheStoragePolicy]; [request setDelegate: self]; [request startAsynchronous];
Html cache completed. For more information, click ASIHTTPRequest, a part of IOS development network.
2. Image caching in html
(1) obtain all image URLs in html code through regular expressions.
NSString * urlPattern = @ "] +? Src = [\ "']? ([^> '\ "] +) [\"']? "; NSError * error = [NSError new]; NSRegularExpression * regex = [NSRegularExpression regularExpressionWithPattern: urlPattern options: NSRegularExpressionCaseInsensitive error: & error]; // match this piece of content is very powerful NSUInteger counts = [regex numberOfMatchesInString: content options: NSRegularExpressionCaseInsensitive range: NSMakeRange (0, [content length])]; // Number of matched times if (counts> 0) {NSArray * matches = [regex matchesI NString: content options: NSMatchingReportCompletion range: NSMakeRange (0, [content length])]; for (NSTextCheckingResult * match in matches) {NSInteger count = [match numberOfRanges]; // match for (NSInteger index = 0; index <count; index ++) {nsange halfRange = [match rangeAtIndex: index]; if (index = 1) {// [listImage addObject: [content substringWithRange: halfRange]; NSLog (@ "converted string ==%@", [content subs TringWithRange: halfRange]); [listImage addObject: [content substringWithRange: halfRange] ;}}// after traversing, You can see three ranges, 1 and 1. 2. The content that matches ([\ w-] +. 3. ([\ w. % & =-] *) matched content}
(2) SDwebImage download and JS interaction replacement
Dispatch_queue_t queue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul); dispatch_group_t group = dispatch_group_create (); for (int I = 0; I <listImage. count; I ++) {NSString * imageUrl = [imageUrlArray objectAtIndex: I]; NSString * key = [self: imageUrl]; UIImage * cachedImage = [[SDImageCache nvidimagecache] imageFromDiskCacheForKey: key]; NSString * index = [NSString stringWithFormat: @ "% d", I]; if (cachedImage) {[tchWebView stringByEvaluatingJavaScriptFromString: [self createSetImageUrlJavaScript: index imgUrl: key];} else {dispatch_group_async (group, queue, ^ {// asynchronously download the image [SDWebImageDownloader. sharedDownloader downloadImageWithURL: [NSURL URLWithString: imageUrl] options: 0 progress: ^ (NSInteger receivedSize, NSInteger expectedSize) {// progression tracking code} completed: ^ (UIImage * image, NSData * data, NSError * error, BOOL finished) {if (image & finished) {[[SDImageCache nvidimagecache] storeImage: image forKey: key]; dispatch_sync (unlock (), ^ {[tchWebView stringByEvaluatingJavaScriptFromString: [self createSetImageUrlJavaScript: index imgUrl: key] ;}}] ;}}} dispatch_release (group );
// Set the downloaded image to the web img-(NSString *) createSetImageUrlJavaScript :( NSString *) index imgUrl :( NSString *) url {UIImage * myCachaImage = [[SDImageCache nvidimagecache] imageFromDiskCacheForKey: url]; NSData * imageData = UIImageJPEGRepresentation (myCachaImage, 1.0); NSString * imageSource = [NSString stringWithFormat: @ "data: image/jpg; base64, % @", [imageData base64Encoding]; NSString * js = [NSString stringWithFormat: @ "var imgArray = document. getElementsByTagName ('img '); imgArray [% @]. src = \ "% @ \"; ", index, imageSource]; return js ;}
// 32-bit MD5 encryption method-(NSString *) getMd5_32Bit_String :( NSString *) srcString {const char * cStr = [srcString UTF8String]; unsigned char digest [digest]; CC_MD5 (cStr, strlen (cStr), digest); NSMutableString * result = [NSMutableString stringWithCapacity: Limit * 2]; for (int I = 0; I <CC_MD5_DIGEST_LENGTH; I ++) [result appendFormat: @ "% 02x", digest [I]; return result ;}
Reference: http://bbs.csdn.net/topics/390831054