2011-06-01 40 views
4

我想使用ASIWebPageRequest加载本地html文件,以便我可以使用ASIWebPageRequest中的内置缓存。我知道这可能听起来有点毫无意义,但是我想在本地文件中使用远程图像,每周只更新一次。如果这是有道理的。ASIHTTPRequest和ASIWebPageRequest加载本地HTML文件错误

下面是我在做什么代码:

//OUTPUT: file:///Users/ledixonuk/Library/Application%20Support/iPhone%20Simulator/4.3.2/Applications/FDF21331-CC80-4ECB-9A33-16AEC073D117/Documents/ItemOne.html 
NSURL *fileUrl = [[NSURL alloc] initFileURLWithPath:[FileSystemHelper dataFilePathInDocuments:@"ItemOne.html"]]; 

// Assume request is a property of our controller 
// First, we'll cancel any in-progress page load 
[[self webRequest] setDelegate:nil]; 
[[self webRequest] cancel]; 

[self setWebRequest:[ASIWebPageRequest requestWithURL:fileUrl]]; 
[[self webRequest] setDelegate:self]; 
[[self webRequest] setDidFailSelector:@selector(webPageFailed:)]; 
[[self webRequest] setDidFinishSelector:@selector(webPageFinished:)]; 

// Tell the request to replace urls in this page with local urls 
//[[self webRequest] setUrlReplacementMode:ASIReplaceExternalResourcesWithLocalURLs]; 

// Tell the request to embed external resources directly in the page 
[[self webRequest] setUrlReplacementMode:ASIReplaceExternalResourcesWithData]; 

// It is strongly recommended you use a download cache with ASIWebPageRequest 
// When using a cache, external resources are automatically stored in the cache 
// and can be pulled from the cache on subsequent page loads 
[[self webRequest] setDownloadCache:[ASIDownloadCache sharedCache]]; 

// Ask the download cache for a place to store the cached data 
// This is the most efficient way for an ASIWebPageRequest to store a web page 
[[self webRequest] setDownloadDestinationPath:[[ASIDownloadCache sharedCache] pathToStoreCachedResponseDataForRequest:[self webRequest]]]; 

[[self webRequest] startAsynchronous]; 

,这里是错误我得到:

404未找到

未找到

请求的URL /用户/ ledixonuk /库/应用程序支持/ iPhone模拟器/ 4.3.2/Ap在此服务器上找不到FDF21331-CC80-4ECB-9A33-16AEC073D117/Documents/ItemOne.html。

显然ASIWebPageRequest无法找到应用程序文件夹中的文件ItemOne.html但是我仔细检查过,文件肯定是有。

有没有其他人有类似的问题呢?这让我疯狂地试图把它解决掉!

回答

1

ASIWebPageRequest是ASIHTTPRequest的一个子类,不幸的是ASIHTTPRequest不支持抓取文件:URL - 只有http和https。

基本上你有所有你需要的代码,你只需要找到一种方法来将它们连接在一起。你可以创建一个ASIHTTPRequest的子类,它可以加载文件:URL(本质上提供你自己的startAsyncronous实现,而不是在设置正确的数据文件后调用代理),或者创建一个ASIWebPageRequest的子类。我还没有仔细考虑过,所以我不知道哪条路最好。

(我想你所看到的404页面ASIHTTPRequest是管理某处联系Web服务器 - 也许它正试图从http://127.0.0.1/Users/ledixonuk/Library/获取....)

+0

我测试过这个,它有同样的问题,谢谢。 – AggroPanda 2011-06-14 08:35:27

0

见简单的实现我已经添加到下面我的ASIHTTPRequest的子类为了支持file://基于的URL。它不是完整的,可能缺少一些应该设置的属性,它不会调用所有代表,但为了我自己的目的,这已经足够了。

- (void)startRequest 
{   
    if ([url isFileURL]) 
    { 
     // ASIHTTPRequest does not support handling file:// URLs, this is my own simple implementation here 

     if ([self isCancelled]) { 
      return; 
     } 

     [self performSelectorOnMainThread:@selector(requestStarted) withObject:nil waitUntilDone:[NSThread isMainThread]]; 

     NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease]; 
     NSString *filePath = [url path]; 
     BOOL isDirectory = NO; 

     if ([fileManager fileExistsAtPath:filePath isDirectory:&isDirectory] && !isDirectory) 
     { 
      responseStatusCode = 200; 
      [self setRawResponseData:[NSData dataWithContentsOfFile:filePath]]; 
      [self setContentLength:rawResponseData.length]; 
      [self setTotalBytesRead:[self contentLength]]; 
     } 
     else 
     { 
      responseStatusCode = 404; 
      [self setContentLength:0]; 
      [self setError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIFileManagementError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Cannot open file at path '%@'",filePath],NSLocalizedDescriptionKey,error,NSUnderlyingErrorKey,nil]]]; 
     } 

     complete = YES; 
     downloadComplete = YES; 

     [self requestFinished]; 
     [self markAsFinished]; 
    } 
    else 
    { 
     // let the original implementation deal with all the other URLs 
     [super startRequest]; 
    } 
}