2

我创建了一个UIWebView在 iOS 应用程序中离线运行的程序。我正在使用某个 javascript 库来显示虚拟游览,这就是我需要使用UIWebView.

我现在在运行应用程序时遇到了一个问题:大部分时间它都在显示旧图像,即使它们不在资源中或已更新。我已经尝试了很多方法来解决这个问题,包括设置缓存策略:

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"splash2" ofType:@"html"]isDirectory:NO];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[self.webView loadRequest:request];

在我的最上面,我viewDidLoad尝试设置缓存容量:

NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
    [NSURLCache setSharedURLCache:sharedCache];

这在我的应用程序 didFinishLaunchingWithOptions 上:

[[NSURLCache sharedURLCache] removeAllCachedResponses];

目前我在我的viewDidLoad

NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
    [NSURLCache setSharedURLCache:sharedCache];


    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"splash2" ofType:@"html"]isDirectory:NO];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
    [self.webView loadRequest:request];

    webView.frame = CGRectMake(0, 0, 1024, 768);

    webView.scalesPageToFit = NO;

    webView.multipleTouchEnabled = YES;

    for (id subview in webView.subviews)
        if ([[subview class] isSubclassOfClass: [UIScrollView class]])
            ((UIScrollView *)subview).bounces = NO;


    [super viewDidLoad];

我也试过我的viewDidLoad

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"splash2" ofType:@"html"]isDirectory:NO]];

    webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];


    NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
    [NSURLCache setSharedURLCache:sharedCache];


    [webView loadRequest:request];
    [self.view addSubview:webView];


    webView.scalesPageToFit = NO;

    webView.multipleTouchEnabled = YES;

    for (id subview in webView.subviews)
        if ([[subview class] isSubclassOfClass: [UIScrollView class]])
            ((UIScrollView *)subview).bounces = NO;


    [super viewDidLoad];

另外我不确定它是否与 html5 清单有关。总而言之,我只需要它来关闭缓存并始终读取我的资源中当前存在的内容。

4

0 回答 0