2013-10-09 61 views
0

我加載一個UIWebView用下面的代碼:如何在線下緩存遠程URL?

NSString *fullURL = @"http://www.example.com"; 
    NSURL *url = [NSURL URLWithString:fullURL]; 
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
    [webview loadRequest:requestObj]; 

是否有任何手段來緩存網站,所以其可脫機使用,如果沒有連接?

+0

你想緩存整個網站 –

+0

取決於..如果我加載一個url,它應該能夠緩存它.. – veereev

回答

0

的關鍵是: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]; 

希望這會有所幫助。

+0

其加載只有HTML我看不到任何CSS – veereev