0

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

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

如果没有连接,是否有任何方法可以缓存网站以便离线使用?

4

1 回答 1

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];

希望这会有所帮助。

于 2013-10-09T12:50:20.747 回答