UIWebview一次又一次地加载相同的html页面?

时间:2013-05-29 06:53:49

标签: html ios objective-c caching uiwebview

我的viewcontroller中有UIWebView,当我尝试打开epub文件时,它正确显示,但此后每个epub显示相同的封面(第一页),其内容不同。

为什么会这样?代码中是否有缓存?

在章课

- (void) loadChapterWithWindowSize:(CGRect)theWindowSize fontPercentSize:(int) theFontPercentSize
{
    //  [[NSURLCache sharedURLCache] removeAllCachedResponses];
    fontPercentSize = theFontPercentSize;
    windowSize =theWindowSize;
    // NSLog(@"webviewSize: %f * %f, fontPercentSize: %d", theWindowSize.size.width, theWindowSize.size.height,theFontPercentSize);
    UIWebView* webView = [[UIWebView alloc] initWithFrame:theWindowSize];
    [webView setDelegate:self];
    NSURLRequest* urlRequest = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:spinePath]];

    //CODE FOR IGNORING CACHE

    //NSURLRequest* urlRequest ;
    //urlRequest=[NSURLRequest requestWithURL:[NSURL fileURLWithPath:spinePath] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10000];

    //[webView loadRequest:urlRequest];
    NSLog(@"%@",spinePath);//html pahe path
    [[UIApplication sharedApplication] openURL:[NSURL fileURLWithPath:spinePath]];
};

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    [webView release];
}

- (void) webViewDidFinishLoad:(UIWebView*)webView
{
    [webView stringByEvaluatingJavaScriptFromString:@"document.open();document.close()"];
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    sharedManager=[Mymanager sharedManager];
    NSString *varMySheet = @"var mySheet = document.styleSheets[0];";
    NSString *addCSSRule =  @"function addCSSRule(selector, newRule) {"
    "if (mySheet.addRule) {"
    "mySheet.addRule(selector, newRule);"                               // For Internet Explorer
    "} else {"
    "ruleIndex = mySheet.cssRules.length;"
    "mySheet.insertRule(selector + '{' + newRule + ';}', ruleIndex);"   // For Firefox, Chrome, etc.
    "}"
    "}";

    // NSLog(@"w:%f h:%f", webView.bounds.size.width, webView.bounds.size.height);

    NSString *insertRule1 = [NSString stringWithFormat:@"addCSSRule('html', 'padding: 0px; height: %fpx; -webkit-column-gap: 0px; -webkit-column-width: %fpx;')", webView.frame.size.height, webView.frame.size.width];
    NSString *insertRule2 = [NSString stringWithFormat:@"addCSSRule('p', 'text-align: justify;')"];
    NSString *setTextSizeRule = [NSString stringWithFormat:@"addCSSRule('body', '-webkit-text-size-adjust: %d%%;')",fontPercentSize];


    [webView stringByEvaluatingJavaScriptFromString:varMySheet];
    [webView stringByEvaluatingJavaScriptFromString:addCSSRule];
    [webView stringByEvaluatingJavaScriptFromString:insertRule1];
    [webView stringByEvaluatingJavaScriptFromString:insertRule2];
    [webView stringByEvaluatingJavaScriptFromString:setTextSizeRule];

    int totalWidth = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollWidth"] intValue];
    pageCount = (int)((float)totalWidth/webView.bounds.size.width);

    //NSLog(@"Chapter %d: %@ -> %d pages", chapterIndex, title, pageCount);

    [webView dealloc];
    [delegate chapterDidFinishLoad:self];
}
我的viewocntroller类中的

- (void) chapterDidFinishLoad:(Chapter *)chapter{
i=i+1;

    NSLog(@"iteration%d",i);
    NSLog(@"%d",[loadedEpub.spineArray count]);
    loadingProgressBar=loadingProgressBar+1;
    sharedManager=[Mymanager sharedManager];
    NSLog(@"%d",sharedManager.coverPageloadCount);
    //sharedManager.coverPageloadCount++;
    totalPagesCount+=chapter.pageCount;
    sharedManager.TotalPageNumber=totalPagesCount;
    if(chapter.chapterIndex + 1 < [loadedEpub.spineArray count]){
        [[loadedEpub.spineArray objectAtIndex:chapter.chapterIndex+1] setDelegate:self];
        [[loadedEpub.spineArray objectAtIndex:chapter.chapterIndex+1] loadChapterWithWindowSize:webView.bounds fontPercentSize:currentTextSize];
        [currentPageLabel setText:[NSString stringWithFormat:@"?/%d", totalPagesCount]];
    } 
    else {
        [currentPageLabel setText:[NSString stringWithFormat:@"%d/%d",[self getGlobalPageCount], totalPagesCount]];
        [pageSlider setValue:(float)100*(float)[self getGlobalPageCount]/(float)totalPagesCount animated:YES];
        paginating = NO;
        //NSLog(@"Pagination Ended!");
}

    - (void)webViewDidFinishLoad:(UIWebView *)thewebView{

    // [self TextureModes];



    NSUserDefaults *menuUserDefaults = [NSUserDefaults standardUserDefaults];
    if([menuUserDefaults boolForKey:@"btnM1"]){
        [webView setOpaque:NO];
        [webView setBackgroundColor:[UIColor whiteColor]];
        NSString *jsString2 = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'black'"];
        [webView stringByEvaluatingJavaScriptFromString:jsString2];

    }
    else{
        NSUserDefaults *userDefaults2 = [NSUserDefaults standardUserDefaults];
        [userDefaults2 setBool:NO forKey:@"btnM1"];
        [userDefaults2 synchronize];

        [webView setOpaque:NO];
        [webView setBackgroundColor:[UIColor blackColor]];
        NSString *jsString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'white'"];
        [webView stringByEvaluatingJavaScriptFromString:jsString];

    }

    if(sharedManager.textureFlag==1)
    {
        webView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"darkWoodP.png"]];
    }else if(sharedManager.textureFlag==2)
    {
        webView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"paperP.png"]];
    }

    NSString *varMySheet = @"var mySheet = document.styleSheets[0];";

    NSString *addCSSRule =  @"function addCSSRule(selector, newRule) {"
    "if (mySheet.addRule) {"
    "mySheet.addRule(selector, newRule);"                               // For Internet Explorer
    "} else {"
    "ruleIndex = mySheet.cssRules.length;"
    "mySheet.insertRule(selector + '{' + newRule + ';}', ruleIndex);"   // For Firefox, Chrome, etc.
    "}"
    "}";


    NSString *insertRule1 = [NSString stringWithFormat:@"addCSSRule('html', 'padding: 0px; height: %fpx; -webkit-column-gap: 0px; -webkit-column-width: %fpx;')", webView.frame.size.height, webView.frame.size.width];
    NSString *insertRule2 = [NSString stringWithFormat:@"addCSSRule('p', 'text-align: justify;')"];
    NSString *setTextSizeRule = [NSString stringWithFormat:@"addCSSRule('body', '-webkit-text-size-adjust: %d%%;')", currentTextSize];
    NSString *setHighlightColorRule = [NSString stringWithFormat:@"addCSSRule('highlight', 'background-color: yellow;')"];


    [webView stringByEvaluatingJavaScriptFromString:varMySheet];

    [webView stringByEvaluatingJavaScriptFromString:addCSSRule];

    [webView stringByEvaluatingJavaScriptFromString:insertRule1];

    [webView stringByEvaluatingJavaScriptFromString:insertRule2];

    [webView stringByEvaluatingJavaScriptFromString:setTextSizeRule];

    [webView stringByEvaluatingJavaScriptFromString:setHighlightColorRule];
    NSString *padding = @"document.body.style.padding='30px 30px 30px 30px';";
    NSString *paddingtop = @"document.body.style.padding-top='85px';";
    NSString *paddingbottom = @"document.body.style.margin-bottom='85px';";
    [webView stringByEvaluatingJavaScriptFromString:padding];
    [webView stringByEvaluatingJavaScriptFromString:paddingtop];
    [webView stringByEvaluatingJavaScriptFromString:paddingbottom];
    if(currentSearchResult!=nil){
        //  NSLog(@"Highlighting %@", currentSearchResult.originatingQuery);
        [webView highlightAllOccurencesOfString:currentSearchResult.originatingQuery];
    }


    totalWidth = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollWidth"] intValue];
    //  NSLog(@"%d %f",totalWidth,webView.bounds.size.width);
    pagesInCurrentSpineCount = (int)((float)totalWidth/webView.bounds.size.width);
    [self gotoPageInCurrentSpine:currentPageInSpineIndex];
}

1 个答案:

答案 0 :(得分:0)

尝试实验:不是重新加载Web视图,而是将其释放并替换为新视图(正确地指定和设置委托)。如果新的Web视图显示相同的内容,则必须是文件。如果这可以修复你现在拥有的一种方法来修复它。