如何缓存远程可用的远程URL?

时间:2013-10-09 12:42:08

标签: ios caching uiwebview offline-caching

我正在使用以下代码加载uiwebview:

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

是否有任何方法可以缓存网站,以便在没有连接时可以脱机使用?

1 个答案:

答案 0 :(得分: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];

希望这会有所帮助。