2014-04-11 4 views
0

나는 UIWebView을 오프라인으로 작업하고 싶습니다. 처음 URL을 방문했을 때 아래 코드를 수행하여 webViewDidFinishLoad 메서드의 문서 디렉토리에 데이터를 저장했습니다.UIWebView lodadata 메서드에서 NSData가 제대로로드되지 않았습니다.

NSCachedURLResponse* response = [[NSURLCache sharedURLCache]          cachedResponseForRequest:[webview request]]; 
NSData * data = [response data]; 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil; 
NSString *filePath = [NSString stringWithFormat:@“%@temp”, basePath]; 
if (response) 
{ 
    BOOL isSaveFile = [NSKeyedArchiver archiveRootObject:data toFile:filePath]; 
} 

두 번째로 webview을 열면 해당 파일이 존재하는지 확인합니다. 내가 로컬에서 파일을 열려면 아래 코드를 완료 한 파일이 있습니다.

NSData * sData = (NSData *)[NSData dataWithContentsOfFile:filePath]; 
if (sData.length) 
{ 
[webview loadData: sData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@""]]; 
} 

위 코드가 제대로 작동하지 않고 아래 출력이 표시됩니다.

enter image description here

나를 인도 해주십시오. 내가 뭘 잘못하고있어 ??

+0

있습니다로드? – sage444

+0

@ sage444 고정되어 있지 않지만 대부분은 .pdf 파일 또는 일부 시간 웹 사이트 URL을 얻고 있습니다. – Hitarth

답변

2

UIWebView은 원시 데이터를 렌더링하는 방법을 모르기 때문에 MIMEType을 올바르게 설정해야합니다.

NSCachedURLResponse* response = [[NSURLCache sharedURLCache] cachedResponseForRequest:[webview request]]; 
NSString *MIMEType = response.response.MIMEType; 
NSData * data = [response data]; 

그런 다음 데이터가 "text/html과"가 있는지 확인

[webview loadData: sData MIMEType:MIMEType textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@""]]; 
+0

MIMEType은 제 실수였습니다. @ "text/html"의 @ "application/pdf"insted였습니다. 감사합니다. – Hitarth