2012-03-04 3 views
1

나는 항상 다음 코드를 사용하여있는 UIWebView의 최신 캐시 된 복사본까지이 있는지 확인하기 위해 노력하고있어 : 내가 종료하지 않고 비행기 모드에 갈 때UIWebView 캐시 검색 중?

// Set URL to help file on server 
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@", HELP_FILE_URL]]; 

// Check network reachability 
wifiReach = [Reachability reachabilityWithHostName:[NSString stringWithFormat:@"%@", SERVER_URL]]; 
netStatus = [wifiReach currentReachabilityStatus]; 

// Start activity indicators 
[self startAnimation]; 

// Verify current help file cache if we have network connection... 
if (netStatus == ReachableViaWWAN || netStatus == ReachableViaWiFi) { 

    helpFileRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadRevalidatingCacheData timeoutInterval:30]; 

} else { 

    // Network NOT reachable - show (local) cache if it exists 
    helpFileRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataDontLoad timeoutInterval:30]; 
} 

// Show help file in a web view 
[webView loadRequest:helpFileRequest]; 

그것은을 제외하고 대부분의 경우 잘 작동을 앱. 비행기 모드에서 캐싱 된 webView는 잘 보이지만 UIWebView 대리인

(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error 

도 내가 원하지 않는 트리거됩니다. 캐시가 비어있는 경우에만 트리거되도록하고 싶습니다! 어떻게하면 될까요? (응용 프로그램을 종료하면 제대로 작동합니다.) 작은 세부 사항이지만 제대로 작동하고 싶습니다.

답변

4

OK - UIWebView 대리자 메서드에서 오류 코드를 식별하여 해결했습니다. 아래를 참조하십시오. 캐시가 비어 있거나 ("리소스를 사용할 수 없음") 오류 코드가 -1008이고 캐시에 데이터가있는 -1009 인 것을 발견했습니다 ("인터넷 연결이 오프라인으로 나타납니다."). 두 경우 모두 오프라인 모드이며 비행기 모드입니다.

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error 
{ 
    NSLog(@"%@ : %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd)); 

    [self stopAnimation]; 

    // No help to display - cache is empty and no Internet connection... 
    if ([error code] == -1008) { 
     NSString *alertMessage = @"To make Help available offline, you need to view it at least once when connected to the Internet."; 
     UIAlertView *alertView = 
     [[UIAlertView alloc] initWithTitle:@"Help Unavailable" 
           message:alertMessage 
           delegate:nil 
        cancelButtonTitle:@"OK" 
        otherButtonTitles:nil]; 
     [alertView show]; 
    } 

    NSLog(@"Error code:%d, %@", [error code], [error localizedDescription]); 
}