1

次のコードを使用して、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

1 に答える 1

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]);
}
于 2012-03-05T14:35:24.490 に答える