ios中UIWebview和asiHttprequest的用法

原文地址为:http://www.cnblogs.com/pengyingh/articles/2343062.html
asiHttprequest的用法 它对Get请求的响应数据进行缓存(被缓存的数据必需是成功的200请求):
ASIHTTPRequest会自动保存访问过的URL信息,并备以后用。在如下几个场景很是有用:
1,当没有网络链接的时候。
2,已下载的数据再次请求时,仅当它与本地版本不样时才进行下载。

ASIDownloadCache 设置下载缓存
它对Get请求的响应数据进行缓存(被缓存的数据必需是成功的200请求):

[ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]]; 当设置缓存策略后,全部的请求都被自动的缓存起来。

另外,若是仅仅但愿某次请求使用缓存操做,也能够这样使用: ASIHTTPRequest
*request = [ASIHTTPRequest requestWithURL:url]; [request setDownloadCache:[ASIDownloadCache sharedCache]];

多种的缓存并存 仅仅须要建立不一样的ASIDownloadCache,并设置缓存所使用的路径,并设置到须要使用的request实例中: ASIDownloadCache
*cache = [[[ASIDownloadCache alloc] init] autorelease]; [cache setStoragePath:@"/Users/ben/Documents/Cached-Downloads"]; [self setMyCache:cache]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setDownloadCache:[self myCache]];
缓存策略
缓存策略是咱们控制缓存行为的主要方式,如:何时进行缓存,缓存数据的利用方式。
如下是策略可选列表(可组合使用):

ASIUseDefaultCachePolicy    这是一个默认的缓存策略“ASIAskServerIfModifiedWhenStaleCachePolicy”,这个很明白,见名知意(它不能与其它策略组合使用)
ASIDoNotReadFromCacheCachePolicy    所读数据不使用缓存
ASIDoNotWriteToCacheCachePolicy    不对缓存数据进行写操做
ASIAskServerIfModifiedWhenStaleCachePolicy    默认缓存行为,request会先判断是否存在缓存数据。a, 若是没有再进行网络请求。 b,若是存在缓存数据,而且数据没有过时,则使用缓存。c,若是存在缓存数据,但已通过期,request会先进行网络请求,判断服务器版本与本地版本是否同样,若是同样,则使用缓存。若是服务器有新版本,会进行网络请求,并更新本地缓存
ASIAskServerIfModifiedCachePolicy    与默认缓存大体同样,区别仅是每次请求都会 去服务器判断是否有更新
ASIOnlyLoadIfNotCachedCachePolicy    若是有缓存在本地,无论其过时与否,总会拿来使用
ASIDontLoadCachePolicy    仅当有缓存的时候才会被正确执行,若是没有缓存,request将被取消(没有错误信息)
ASIFallbackToCacheIfLoadFailsCachePolicy    这个选项常常被用来与其它选项组合使用。请求失败时,若是有缓存当网络则返回本地缓存信息(这个在处理异常时很是有用)
    若是设置了“defaultCachePolicy”则全部的请求都会使用此缓存。
缓存存储方式
你能够设置缓存的数据须要保存多长时间,ASIHTTPRequest提供了两种策略:
a,ASICacheForSessionDurationCacheStoragePolicy,默认策略,基于session的缓存数据存储。当下次运行或[ASIHTTPRequest clearSession]时,缓存将失效。
b,ASICachePermanentlyCacheStoragePolicy,把缓存数据永久保存在本地,
如:

ASIHTTPRequest *request = [ ASIHTTPRequest requestWithURL:url ];
[ request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy ];
另外,也可使用clearCachedResponsesForStoragePolicy来清空指定策略下的缓存数据。

缓存其它特性
设置是否按服务器在Header里指定的是否可被缓存或过时策略进行缓存:

[[ ASIDownloadCache sharedCache ] setShouldRespectCacheControlHeaders:NO ];
设置request缓存的有效时间:

[ request setSecondsToCache:60*60*24*30 ]; // 缓存30天
能够判断数据是否从缓存读取:

[ request didUseCachedResponse ];
设置缓存所使用的路径:

[ request setDownloadDestinationPath:[[ ASIDownloadCache sharedCache ] pathToStoreCachedResponseDataForRequest:request ]];
实现自定义的缓存
只要简单的实现ASICacheDelegate接口就能够被用来使用。

 








#import <UIKit/UIKit.h> #import "ASIDownloadCache.h" @class ViewController; @interface AppDelegate : UIResponder <UIApplicationDelegate>{ } @property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic) ViewController *viewController; @property(nonatomic,retain)ASIDownloadCache *myCache; @end @implementation AppDelegate - (void)dealloc { [_window release]; [_viewController release]; [_myCache release]; [super dealloc]; } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //声明一个全局的的缓存 //自定义一个缓存 ASIDownloadCache *cache=[[ASIDownloadCache alloc] init]; self.myCache=cache; [cache release]; //设置缓存路径 NSString *cachePath=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; [self.myCache setStoragePath:[cachePath stringByAppendingPathComponent:@"resource"]]; [self.myCache setDefaultCachePolicy:ASIDoNotReadFromCacheCachePolicy];
-(void)click{
    NSURL *url=[NSURL URLWithString:@"http://www.baidu.com"];
    //NSURLRequest *request=[NSURLRequest requestWithURL:url];
//    [webview loadRequest:request];
    ASIHTTPRequest *request=[ASIHTTPRequest requestWithURL:url];
    //获取全
    AppDelegate *appdelegate=[UIApplication sharedApplication].delegate;
    //设置缓存方式
    [request setDownloadCache:appdelegate.myCache];
    //设置缓存数据存储策略,这里若是无更新和没法联网就读取缓存数据。
    [request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
    request.delegate=self;
    [request startAsynchronous];
}

-(void)requestStarted:(ASIHTTPRequest *)request{
    NSLog(@"%@",request.responseString);
}

#pragma mark -请求数据
-(void)requestFinished:(ASIHTTPRequest *)request{
    NSLog(@"requestfinish---->%@",request.responseString);
   [webview loadHTMLString:request.responseString baseURL:nil];
}
相关文章
相关标签/搜索