ASI 라이브러리를 이용한 파일 다운로드 & 캐쉬 하는 법을 소개합니다.
ASI 라이브러리는 아주 강력합니다. 한번씩 사용해보시는 것을 추천 드립니다.
[sourcecode language="ObjC"]
- (IBAction)downloadWithCache:(id)sender {
NSURL *url = [NSURL URLWithString:@"http://img1.ruliweb.daum.net/img2/cmu_tv/11278_3.jpg"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[request setCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];
[request setSecondsToCache:60*60*24*30]; // Cache for 30 days
[request setDelegate:self];
// you can use synchronous how below code.
// [request startSynchronous];
[request startAsynchronous];
BOOL success = [request didUseCachedResponse];
NSLog(@"[///] Success is %@\n", (success ? @"YES" : @"NO"));
NSData *responseData = [request responseData];
// you can do that.
}
// if use asynchronous, you must use the below code that call-back methods.
- (void)requestFinished:(ASIHTTPRequest *)request {
BOOL success = [request didUseCachedResponse];
NSLog(@"[///] Success is %@\n", (success ? @"YES" : @"NO"));
NSData *responseData = [request responseData];
}
[/sourcecode]