How to cache content in UIWebView for faster loading later on?

后端 未结 4 999
醉话见心
醉话见心 2020-11-27 15:02

I notice that the iphone safari caches content so that your page load for later is much faster much like a desktop browser. So take mobile gmail web page for example, the fi

4条回答
  •  伪装坚强ぢ
    2020-11-27 15:30

    The key is: NSURLRequestReturnCacheDataElseLoad

    NSData *urlData;
    NSString *baseURLString =  @"mysite.com";
    NSString *urlString = [baseURLString stringByAppendingPathComponent:@"myfile"];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval: 10.0]; 
    NSURLConnection *connection=[[NSURLConnection alloc] initWithRequest:request delegate:nil]; 
    
    if (connection)
    { 
        urlData = [NSURLConnection sendSynchronousRequest: request];
    
        NSString *htmlString = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
        [webView loadHTMLString:htmlString baseURL:baseURLString];
        [htmlString release];
    }
    
    [connection release];
    

提交回复
热议问题